6

I have a PHP script sitting on a server that is hit by several different machines at different times throughout the day based on cronjobs that are setup on each machine. I'd like to know the IP of the machines making the request and when it is made by a browser, the following executes successfully:

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

However, when made by CURL or any other command line tool I have attempted to use (lynx included), I end up with the following garbage:

2701:5:4a80:7d:2ee:8eff:5e61:801d

From the investigation I've done, this is a result of Apache not populating the $_SERVER variable for requests received that are made from the command line.

REMOTE ADDR Issue with Cron Job

Anyone know of a way to get command line requests to play nice with the $_SERVER variable or should I go down another route?

Community
  • 1
  • 1
TMan
  • 1,775
  • 1
  • 14
  • 26

1 Answers1

10

That's not garbage, that is the correct remote address. Someone used IPv6 to access your server.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • My bad :-\ Any idea how to get the associated IPv4 address? – TMan Sep 24 '12 at 18:54
  • @TMan, It's not possible. IPv4 and IPv6 don't necessarily have to have anything to do with each other. – Brad Sep 24 '12 at 18:55
  • Perfect, thanks for the help! I'm guessing I would have to force IPv4 on the client computer to get the address I'm looking for then? – TMan Sep 24 '12 at 18:59
  • 2
    check [the accepted answer of this thread](http://stackoverflow.com/questions/1555591/how-to-convert-an-address-from-ipv4-to-ipv6) for help on checking IPv4 against IPv6 addresses... it's a tricky business to say at least – Zathrus Writer Sep 24 '12 at 19:13
  • 2
    For anything you develop now the safest way forward is to support both IPv4 and IPv6. You will encounter both of them at some point in time, and adding IPv6 support afterwards is so much more work... – Sander Steffann Sep 25 '12 at 07:15