0

I would like to get the visitor's ip address.

Which one is the appropriate one to use:

Request.ServerVariables("REMOTE_ADDR") 

or

Request.UserHostAddress

Which one is the best method?

Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114
  • 1
    possible duplicate of [How to Get IP Address?](http://stackoverflow.com/questions/1907195/how-to-get-ip-address) – Alastair Pitts Jul 12 '12 at 05:22
  • @AlastairPitts - No its not The question here is that which one of them to use ? And not how to Get Ip Adress . – bhuvin Jul 12 '12 at 06:55

3 Answers3

1

They're exactly the same. UserHostAddress simply calls GetRemoteAddress.

public string UserHostAddress
{
    get
    {
        if (this._wr != null)
        {
            return this._wr.GetRemoteAddress();
        }
        return null;
    }
}

public override string GetRemoteAddress()
{
    return this.GetServerVariable("REMOTE_ADDR");
}

Both are also supported from .NET Framework 1.0/1.1 onward.

Jim
  • 1,695
  • 2
  • 23
  • 42
0

both will return the same value as an IP-Address i would prefer Request.UserHostAddress because it's definitely supported from .net framework 1.0 to 4.5

take a look: MSDN

Community
  • 1
  • 1
Cadburry
  • 1,844
  • 10
  • 21
-3

Try using

phpinfo();

It should give you something like REMOTE_ADDR which is the variable you need to grab in your php script. This should be passed from the webserver to your PHP script.

Then use:

$ip = getenv('REMOTE_ADDR');