0

I am trying to use the time and IP address to generate unique ID for users as they register for the site. The DB I'm using is MySQL and it comes with auto increment, but it doesn't seem like a practical technique for this. I am having issues with $_SERVER['REMOTE_ADDR'] returning an unreadable address. I'm getting symbols for the value in the firebug console. I tried using inet_ntop and inet_pton, neither worked.

$ip_address = $_SERVER['REMOTE_ADDR'];
$test = inet_ntop($ip_address);
echo($test);

Why am I getting symbols instead of readable text?

EDIT:

What I want to store is a combination of the IP and time. I need the IP to show in format of "79.104.97.105" -Niet the Dark Absol, but what I'm getting is ��������������� if I use use inet_ntop or inet_pton or ::1 if I use just $_SERVER['REMOTE_ADDR'].

Two part question: 1) Am I getting the IP address as IPv6 if it returns ::1, 2) How do I convert to 127.0.0.1 which I think is IPv4

user2812097
  • 92
  • 2
  • 14

2 Answers2

4

inet_ntop() expects the address in a binary format.

Example:

$packed = chr(127) . chr(0) . chr(0) . chr(1);
$expanded = inet_ntop($packed);

/* Outputs: 127.0.0.1 */
echo $expanded;

You need to use inet_pton() instead, which expects the IP in for format in $_SERVER['REMOTE_ADDR'].

Update

In the question you state that you need the address in readable format.

echo $_SERVER['REMOTE_ADDR'];

The variable already contains the IP in human readable format, no need for any PHP function to convert it.

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
  • When I use that this is the return statement: ��������������� – user2812097 Oct 01 '14 at 13:53
  • Well, `inet_pton` converts the IP into binary format, so you see this. Maybe you should expand the question on what you actually want to store in the database. – Gerald Schneider Oct 01 '14 at 13:55
  • @GeraldSchneider He edited the question: `::1 if I use just $_SERVER['REMOTE_ADDR'].` – Daniel W. Oct 01 '14 at 14:08
  • Well, one can just accept that `::1` is just as valid as `127.0.0.1`. – Gerald Schneider Oct 01 '14 at 14:09
  • I think I needed both, I was getting the symbols and the ::1 I wasn't sure if I was doing it right because anywhere I look said it was 127.0.0.1 no matter if IPv4 or IPv6. Now that I know for sure that I'm getting the IP, how do I convert it? Guess it was a two part question. – user2812097 Oct 01 '14 at 14:17
0

You can try this function :

function getRealIP(){
    if (!empty($_SERVER['HTTP_CLIENT_IP'])){
      $ip = $_SERVER['HTTP_CLIENT_IP'];
    }else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
      $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
      $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

EDIT

This question may help you I think he had the same issue : Should a MAMP return ::1 as IP on localhost?

Community
  • 1
  • 1
Sebastien B.
  • 476
  • 3
  • 10
  • It's always good to check all headers for proxies +1, but the answer 1. has no explanation 2. is not related to the OPs question -1. – Daniel W. Oct 01 '14 at 13:54
  • Well, OP said : `I am having issues with $_SERVER ['REMOTE_ADDR'] returning an unreadable address.` when I echo the function a printed here I get clear IP address @127. 0.0.1 so to me, it's related sorry if isn't wasn't a bad intention. – Sebastien B. Oct 01 '14 at 14:00
  • Well, OP has written a confusing question there but read his edit. He is getting `::1` from `$_SERVER['REMOTE_ADDR']` – Daniel W. Oct 01 '14 at 14:09
  • Thank for your comment I have edited my answer to link a similar problem. – Sebastien B. Oct 01 '14 at 14:21
  • I tried inet_ntop like in the link you post in your edit and I'm getting the symbols. Why am I getting symbols and not 127.0.0.1? – user2812097 Oct 01 '14 at 14:35
  • hmm maybe the page charset ? try to set `charset=utf-8` html version : `` php version : `header('Content-Type: text/html; charset=utf-8');` – Sebastien B. Oct 01 '14 at 14:55