PHP/Cron Based IP Tracker

October 27, 2006

I connect to my home machine from work and school fairly regulary, I’d say at least three times a week, often more. The problem is, COX runs DHCP and every once in a while my lease expires and I get a new IP. It’s not too often, but when I can no longer get to my machine it’s frustrating.

No more comrades. I wrote this little PHP script to handle the problem, and the really neat part (I think) is that it uses itself for the data storage. Slick, I know. It’s an idea much like the no-ip and dyndns services. Anyway, here it is, you can just hook up a cron job to keep it up to date.

If you wanted to you could add simple authentication, though if a script-kiddie wants to waste his time flooding the thing with incorrect IP’s, what do I care? It should fix itself in (insert the time spacing of your cron job here) anyway.

P.S. One caveat here is that thanks to some weird bug or combination of bugs in WP I can’t post PHP code that has fopen, fgets, fclose in it. Thus everywhere you see // HERE in the code I’ve added a space to one of those function calls that needs fixing. It’d probably be easier just to grab the source at this link.

Update (11/01/06)
I didn’t check the operation of this script enough and it turns out there was a bug, at least in my implementation. It was adding a newline and not removing the old IP address, so it was essentially not working. Easily fixed, and my new source has replaced the old at the link (though it has not been changed above).

I also realized that I didn’t throw in the cron part of this in the first posting. I use lynx for the job, though wget works too. Here’s my command, I cron it every hour.
lynx -dump http://www.example.com/iptracker.php?set=1 > /dev/null

Categories: Geek
Tags: , ,

Leave A Comment

Your email will not be published.