Static Links for Dynamic IP

The Problem: You want to host a website on your home computer but your ip address is not fixed and so any link you give someone stops working sooner or later, usually sooner.

The Solution: There are various dynamic dns services, however this method is totally free. The strategy is to have a redirect from a known and constant address to your computer. You will need:

  1. Both http and ftp read/write access to some webspace that has a fixed IP address. There are free providers out there.
  2. PHP installed on your home computer. PHP is a free scripting language. If you don't know what all this means google PHP tutorials.

You will also need to create a PHP file from the following text between the two horizontal lines. Simply copy it, paste into a text editor and save as ipg.php in a folder c:\newcd .


<?php $file = "c:/newcd/ip.txt"; if(!$fh = fopen($file, 'r')){echo "\nCannot open file\n";} if(!$fh){} else { $ip2 = fread($fh, filesize($file)); fclose($fh); } $url = "http://members.iinet.net.au/~metcalfe420/ip.php"; $ip = file_get_contents($url); echo "$ip,$ip2\n"; if($ip != $ip2){ $fh = fopen($file,'w'); fwrite($fh, $ip); fclose($fh); $file="c:/newcd/rd.html"; $fh=fopen($file,'w'); $str = <<<EOF <html><head><title>You are being redirected to Edward's Server, if it's switched on that is</title></head> <body onLoad="S1.submit()"> <form id="S1" name="S1" action="http://$ip:81" method="POST"> <p>You are being redirected to Edward's Server <p>Two things are required: <p>1. Javascript enabled in your browser and <br>2. My home computer needs to be switched on. <script> var query = window.location.href.split('?')[1]; if(query) { params = new Array(); query2 = new Array(); fields = new Array(); nv = new Array(); (params) = query.split('='); for (y in params){ if(y >1){params[1]=params[1]+'='+params[y];} } (query2) = params[1].split('%3F',2); document.getElementById('S1').action='http://$ip:81'+query2[0]; var div=''; if(query2[1]){ (fields)=query2[1].split('%26',2); for (x in fields){ (nv)=fields[x].split('%3D'); div=div+'<input type=hidden name="'+nv[0]+'" value="'+nv[1]+'">'; } document.write(div); } if(params[0]=='a'){S1.action='http://$ip';} } </script> </form></body></html> EOF; fwrite($fh, $str); fclose($fh); $conn_id = ftp_connect("YOUR DOMAIN NAME OF YOUR FTP SERVER GOES IN HERE"); $login_result = ftp_login($conn_id, "USERNAME", "PASSWORD"); $upload = ftp_put($conn_id, "rd.html", "c:/newcd/rd.html", FTP_BINARY); ftp_close($conn_id); } // end of if($ip != $ip2) echo "Done\n"; ?>

You will have to edit your details into the last section where the capitalised letters are.

Then have a batch file to kick open your php script. This is a single liner. Here is the one that I use: ip.bat .


c:\xampp\php\php c:\newcd\ipg.php

I have xampp for windows. Depending on what PHP version you have will depend on the exact contents of this file. This is then set up as a two scheduled tasks. The first task goes off once every hour. The second task when the computer has been idle for a minute. I also run this automatically in the startup menu.

How it all works

ipg.php gets the remote ip address of your modem (ADSL or dialup) from http://members.iinet.net.au/~metcalfe420/ip.php . If this has changed from the last time it checked then it writes a webpage: rd.html with the new ip address embedded in it. In case I change my ISP (no reason to so far) here is ip.php


<?php echo $_SERVER['REMOTE_ADDR']; ?>

Note that ip.php relies on your ISP (or free webhost) to host php. Many free webhosts out there put in an advert somewhere in the page, which is no good for our purposes, so choose carefully.

rd.html is then transferred to the ftp server automatically. It is now at a fixed address which is now willing to redirect to your dynamic ip address.

One more thing: rd.html redirects to your computer on port 81. Your webserver will have to listen on port 81 or you could edit out all occurances of :81 from ipg.php . If you have an ADSL modem you will have to set up IP-Forwarding / Virtual server to make sure the request gets forwarded to your computer and not someone elses in your house.

A few examples

Lets say you have a file called test.html in the webserver root directory. For someone to access this directly he would use the address:
http://[your path to rd.html]/rd.html?q=/test.html

or in my case
http://members.iinet.net.au/~metcalfe420/rd.html?q=/test.html

A few seconds later the user will see something in his address bar like:
http://203.18.45.115:81/test.html

If you are linking into a script file using the get method you will need to use url encoding. So
http://203.18.45.115:81/nodb_loan.php

Becomes
http://members.iinet.net.au/~metcalfe420/rd.html?q=/nodb_loan.php

Good luck with it all.