This is a php script intended to be ran as a cron job with the purpose of keeping a DNS record up to date with a dynamic IP.

dynamic_ip.php

<?php 
	/**
	*	dynamic_ip.php is a cron job designed to check a domain record against the machines public IP.
	*	Should the A record for the specified domain not match the public IP, 
	*	an nsupdate is ran against the nameserver to bring it up to date.
	*
	*	@author John Alder
	*	@website http://jalder.com
	*
	**/

	$domain = 'dynamic.mydomain.tld'; //the dynamic domain you're keeping up to date
	$zone = 'mydomain.tld';	//the zone for the domain
	$ip_url = 'http://jalder.com/ip'; //personal whatismyip service
	$auth_server = 'ns.mynameserver.tld'; //authoritative nameserver you're keeping up to date
	$auth_name = 'dynamic.mydomain.tld';	//authoritative name for key file
	$auth_secret = 'X..X=='; //key secret for nsupdate

	$nsupdate_template = "server ".$auth_server."
key ".$auth_name." ".$auth_secret."
zone ".$zone."
update delete ".$domain.". A
update add ".$domain.". 86400 A %dynamic_ip%
send
";

	$dynamic_ip = file_get_contents($ip_url);

	if(!filter_var($dynamic_ip, FILTER_VALIDATE_IP)){
		die();
	}

	$record = dns_get_record($domain,DNS_A,$auth_server);

	if(isset($record[0]["ip"])){
		if($record[0]["ip"]!=$dynamic_ip){
			$nsupdate_command = str_replace('%dynamic_ip%',$dynamic_ip,$nsupdate_template);
			$nsupdate_file = tempnam("/tmp",'NS');
			$file = fopen($nsupdate_file,"w");
			fwrite($file,$nsupdate_command);
			fclose($file);
			exec('nsupdate -v '.$nsupdate_file);
			unlink($nsupdate_file);
		}
		else{
			die();
		}
	}
	die();