1

Possible Duplicate:
How do I find a user's IP address with PHP?

I have a form in my PHP page. Users fills in his credentials and the message and presses send. I get his question through email and can answer him.

But I would also like to add the public IP of the person to the email, is this possible?

Many thanks!

Community
  • 1
  • 1
CustomX
  • 9,948
  • 30
  • 85
  • 115

4 Answers4

10

Try using $_SERVER['REMOTE_ADDR'] to fetch the user's IP address.

http://php.net/manual/en/reserved.variables.server.php

Steven Mercatante
  • 24,757
  • 9
  • 65
  • 109
  • On my local site i am getting 127.0.0.1. Can't I get my public ip on my local site. –  Sep 07 '12 at 09:40
1

You can use $_SERVER["REMOTE_ADDR"]. But it's not necessarily reliable.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • I'm not aware of any method of spoofing REMOTE_ADDR short of forging IP packets. However, it is worth nothign that REMOTE_ADDR may be the address of a proxy (even one you yourself set up) rather than the remote user. E.g., I have nginx in front of apache, so the REMOTE_ADDR apache receives is that of my nginx server. – Frank Farmer Jun 08 '11 at 22:06
  • @Frank: That's roughly what I was hinting at! – Oliver Charlesworth Jun 08 '11 at 22:07
1

You can use $_SERVER['REMOTE_ADDR'] but if you really want to make sure you are getting the right IP and not the proxy IP then i have found this function to be helpful

Note : i am not the owner of this code, i found it somewhere over the internet

function get_real_ip()
{

    if (isset($_SERVER["HTTP_CLIENT_IP"]))
    {
        return $_SERVER["HTTP_CLIENT_IP"];
    }
    elseif (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
    {
        return $_SERVER["HTTP_X_FORWARDED_FOR"];
    }
    elseif (isset($_SERVER["HTTP_X_FORWARDED"]))
    {
        return $_SERVER["HTTP_X_FORWARDED"];
    }
    elseif (isset($_SERVER["HTTP_FORWARDED_FOR"]))
    {
        return $_SERVER["HTTP_FORWARDED_FOR"];
    }
    elseif (isset($_SERVER["HTTP_FORWARDED"]))
    {
        return $_SERVER["HTTP_FORWARDED"];
    }
    else
    {
        return $_SERVER["REMOTE_ADDR"];
    }
}
Sabeen Malik
  • 10,816
  • 4
  • 33
  • 50
-4

You can use the following code:

$ip = $REMOTE_ADDR;  
Dave
  • 1,303
  • 7
  • 19