Return to Snippet

Revision: 47947
at June 19, 2011 21:23 by madc


Initial Code
/* Setup */
$ipFile = "storeFile.txt";

if( isset($_GET['updateIp']) )
{
	$fh = fopen($ipFile, 'w') or die('Cannot open ipFile.');
	fwrite($fh, getIpAddr());
	fclose($fh);
	
	echo 'IP set.';
}
else
{
	$content = file($ipFile);
	$ip = $content[0];
	
	if( isset($_GET['showIp']) )
		echo showIpAddr($ip);
	else
		redirectIpAddr($ip, 'http','80');
}

function redirectIpAddr($ip, $protocol, $port)
{
	if(empty($ip))
		echo "No IP set.";
	else	
		Header('Location: '.$protocol.'://'.$ip.':'.$port); 
}

function showIpAddr($ip)
{
	$ipInfo = '<h1>IP Status</h1>'.
		'<b>IP Address:</b> '.$ip.' (Last update: '.date('d.m.Y H:i:s',filemtime($ipFile)).')';

	return $ipInfo;
}

function getIpAddr()
{
	# function based on [ http://roshanbh.com.np/2007/12/getting-real-ip-address-in-php.html ]
	if (!empty($_SERVER['HTTP_CLIENT_IP']))
		return $_SERVER['HTTP_CLIENT_IP'];
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
		return $_SERVER['HTTP_X_FORWARDED_FOR'];
	else
		return $_SERVER['REMOTE_ADDR'];
}

Initial URL
http://madcity.at/

Initial Description
Serverside script for a personal DynDNS service.
Works together with a clientside script written in ruby [ http://snipplr.com/view/55484/own-dyndns-service-clientside-script/ ] .

Redirect to current saved IP(http, port 80):
http://dynDns.yourveryownhost.com/

Update with current IP:
http://dynDns.yourveryownhost.com/?updateIp

Show saved IP:
http://dynDns.yourveryownhost.com/?showIp

The getIpAddr-function is based on a blog-post written by Roshan Bhattarai [ http://roshanbh.com.np/2007/12/getting-real-ip-address-in-php.html ]

Initial Title
Own DynDns Service, serverside script.

Initial Tags
server, ip, update

Initial Language
PHP