Skip to page content or Skip to Accesskey List.

Work

Main Page Content

Dynamic Ip Redirection

Rated 3.68 (Ratings: 3)

Want more?

  • More articles in Code
 
Picture of jverde

Joao Verde

Member info

User since: 15 Oct 2003

Articles written: 1

Those working on the internet as webdevelopers or webdesigners more than often find the need to host webservers on their own machines to let their clients follow their work. The problem is that as the IP changes, the address they gave their clients will stop working. To solve that, several companies offer you the chance to update your dynamic IP everytime it changes allowing you to keep a static URL pointing to your working, localhost machine.

If you don't have an internet site I guess you're pretty much stuck with that solution. However, if you do have a site - but keep a running server on your localhost - there is another way to do it using PHP!

Who does this article apply to? To all those who:

  • want to give others access to their machine's webserver;

  • have PHP installed;

  • have a running internet site, preferrably with subdomains.

    On the cable service I'm using, the IP address doesn't change while I'm connected so I need only update my IP address one time, at boot up. The first thing we have to do is to create a PHP script which, at boot time, connects to my internet site and uploads a file containing my current IP address. That way, instead of having to give a new URL each time (each day) the IP changes, I can just say "Hey, visit me at http://myhome.myserver.net!" and they will get to the right place. That's where subdomains come in handy. If you're able to create a subdomain for redirection purposes that's better as http://myhome.myserver.net is a lot fancier and easier to remember than http://www.myserver.net/myhome/.

    So, to update the IP I have a simple batch file called updateip.bat which runs at boot time and goes like this:

    @echo off

    h:\php\php.exe d:\000_LocalhostRedirection\updateip.php

    I suppose you could do it without using a batch file, running PHP itself, but that's an old habit for someone who remembers using MSDOS 3.10. Remember to change those paths to whatever applies to you.

    Now, the script updateip.php:

    <?php

    /*

    UPDATEIP.PHP

    Gets current IP address and updates the server

    */

    // The name should be your machine's name. If you use localhost it will return

    // 127.0.0.1 which doesn't suit our needs. Replace "e;orange"e; by whatever network name your machine has.

    $orange_ip = gethostbyname('orange');

    // Next we create a file containing just the IP address we got before

    $fp = fopen('orange_ip','w');

    fputs($fp,$orange_ip);

    fclose($fp);

    // Whatever we need to connect to the ftp server we set here

    $myserver['ftp'] = 'ftp.yourdomain.net';

    $myserver['user'] = 'yourusername';

    $myserver['pass'] = 'yourpassword';

    // Open the connection

    $ftp = ftp_connect($myserver['ftp']);

    $result = ftp_login($ftp, $myserver['user'], $myserver['pass']);

    // If there is a connection, we send the file. If not, nothing happens.

    // With little more effort we could notify someone if this fails

    // The second parameter for ftp_put is the destination file. Change it to whatever is your case.

    // As this file is to run at startup we echo nothing. It should run silently.

    if ($result)

    {

    $upload = ftp_put($ftp, 'www/orange_ip', 'orange_ip', FTP_ASCII);

    ftp_close($ftp);

    }

    ?>

    Now that we have, on the server, a file with our current IP address what must be done? Create a script which, when called, redirects the visitor to our localhost using our current IP Address. For convenience I named the script index.php. That way it will be called by default when someone types the address I gave them.

    <?php

    /*

    INDEX.PHP

    Reads current IP address and redirects the visitor

    */

    // Remember the file we uploaded with updateip.php?

    $fp = fopen('orange_ip','r');

    $destip = fgets($fp,20);

    fclose($fp);

    // Assuming something could have gone wrong, we check to see if there's something inside the

    // file. Not the perfect method, I agree. Still, I'm lazy.

    // If $destip has something in it (an IP address we hope) we use the HTTP Location directive to

    // redirect the browser.

    if ($destip != '')

    {

    $location = trim('http://'.$destip.'/');

    header("Location: ".$location);

    exit;

    }

    ?>

    And that's it. There is space for improvements. Lazy as I am I didn't take the time to prevent or circumvent errors, but things should work smoothly. When your machine boots, your IP address is sent via FTP to your server. If someone visits the address you gave, they will be redirected to your localhost. If your IP changes frequently and you're using any scheduling software you can make sure updateip.php runs from time to time. Enjoy.

  • A Physical Geographer doomed to PHP programming since 2000.

    The access keys for this page are: ALT (Control on a Mac) plus:

    evolt.org Evolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.