Posted By

madc on 06/19/11


Tagged

server ip update DynDNS


Versions (?)

Own DynDns Service, serverside script.


 / Published in: PHP
 

URL: http://madcity.at/

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 ]

  1. /* Setup */
  2. $ipFile = "storeFile.txt";
  3.  
  4. if( isset($_GET['updateIp']) )
  5. {
  6. $fh = fopen($ipFile, 'w') or die('Cannot open ipFile.');
  7. fwrite($fh, getIpAddr());
  8. fclose($fh);
  9.  
  10. echo 'IP set.';
  11. }
  12. else
  13. {
  14. $content = file($ipFile);
  15. $ip = $content[0];
  16.  
  17. if( isset($_GET['showIp']) )
  18. echo showIpAddr($ip);
  19. else
  20. redirectIpAddr($ip, 'http','80');
  21. }
  22.  
  23. function redirectIpAddr($ip, $protocol, $port)
  24. {
  25. if(empty($ip))
  26. echo "No IP set.";
  27. else
  28. Header('Location: '.$protocol.'://'.$ip.':'.$port);
  29. }
  30.  
  31. function showIpAddr($ip)
  32. {
  33. $ipInfo = '<h1>IP Status</h1>'.
  34. '<b>IP Address:</b> '.$ip.' (Last update: '.date('d.m.Y H:i:s',filemtime($ipFile)).')';
  35.  
  36. return $ipInfo;
  37. }
  38.  
  39. function getIpAddr()
  40. {
  41. # function based on [ http://roshanbh.com.np/2007/12/getting-real-ip-address-in-php.html ]
  42. if (!empty($_SERVER['HTTP_CLIENT_IP']))
  43. return $_SERVER['HTTP_CLIENT_IP'];
  44. elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
  45. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  46. else
  47. return $_SERVER['REMOTE_ADDR'];
  48. }

Report this snippet  

You need to login to post a comment.