PHP DynDNS ip update script

I was looking around for a easy-to-use system to update multiple hostnames at dyndns.com, but found none so I decided to make something myself. And here it is!!!

<?php
$username = "dyndns username";
$password = "dyndns password";
$hosts = array("my first dyndns hostname","my second dyndns hostname","my third dyndns hostname");

$myIP;

function getip()
{
	global $myIP;

	$curl_handle = curl_init();
	curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12");

	curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 2);

	curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
	curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($curl_handle,CURLOPT_URL, "http://checkip.dyndns.org/");
	$buffer = curl_exec($curl_handle);

	if(curl_errno($curl_handle))
	{
		echo 'Curl error: ' . curl_error($curl_handle);
		return -1;
	}
	else
	{
		if (empty($buffer))
		{
			return -1;
		}
		else
		{
			$myIP = substr($buffer, 76, -16);
			$myFile = "store";

			$theData = "";

			if (file_exists($myFile))
			{
				$fh = fopen($myFile, 'r');
				$theData = fread($fh, filesize($myFile));
				fclose($fh);
			}

			if ($theData != $myIP)
			{
				$fh = fopen($myFile, 'w') or die("can't open file");
				$stringData = $myIP;
				fwrite($fh, $stringData);
				fclose($fh);
				return 1;
			}
			else
			{
				return 0;
			}
		}
	}

	curl_close($curl_handle);
}

$status = getip();

if ($status == 1)
{
	echo "Updating:";
	for($i = 0; $i < count($hosts); $i++)
	{
		$curl_handle = curl_init();
		curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12");

		curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 2);

		curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
		curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($curl_handle,CURLOPT_URL, "https://" . $username . ":" . $password . "@members.dyndns.org/nic/update?hostname=" . $hosts[$i] . "&myip=" . $myIP . "&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG");
		$buffer = curl_exec($curl_handle);

		if(curl_errno($curl_handle))
		{
			echo 'Curl error: ' . curl_error($curl_handle);
		}
		else
		{
			if (empty($buffer))
			{
				print "No connection.";
			}
			else
			{
				print "\n\t" . $hosts[$i] . " - " . $buffer;
			}
		}
		curl_close($curl_handle);
	}
}
else if ($status == 0)
{
	echo "No update needed.";
}
else
{
	echo "Could not connect to checkip.dyndns.org";
}
?>

You should be able to add all your hostnames to the $hosts array, and they then all will be updated to the ip the update request was made from.
Before sending an update, it checks the current ip against the “store” file that is holding the last ip the domains was updated to, that way you should not be banned from the system for spamming with updates.

One comment

Leave a Reply