简体   繁体   中英

How to code feature on checking visitor IP address in PHP?

I still confused on this subject and ask for any help and reference on how to check a visitor IP address via PHP.

I'm know it seems lazy to ask something before attempting to code something to show, but right now, I am also googling to find it. Hopefully someone can give general broad answer or some link to read?

BTW, what is to be considered when we have to code some feature like this?

PS: Thank you very much everybody. It's been enlightening arguments. Frankly, I choose the answer more as respect rather than the truth in it. Because until now I still don't know the right one. Maybe I need more years of learning before I get a firm understanding of the topic itself.

请参阅$ _SERVER ,具体如下:

$_SERVER['REMOTE_ADDR'];

OK, justjoe, I see you got confused with this arguement and there is my big part in it.

Some more explanations for you.

The answer depends on the task. You have 2 options:

If you need only one IP address, you can use only REMOTE_ADDR and nothing else. Take a look at the web-server's access log: there is only one IP address and it's REMOTE_ADDR one. At least it guaranteed you a valid IP address. In many cases, like a traffic counter, it's the only thing you can rely on. Thes is general answer to the "How to get an IP address" question.

If you want to record an address that can be more precise probably - so, no one forbids you from recording many addresses, not one. But of course, you have to record these HTTP headers along with REMOTE_ADDR, not instead of it. There is some use for such a throng of addresses. But you can't rely on it too much. But you can dig some information from it, if you care.

The only case for the FORWARDED_FOR header is a misconfigured webserver, who place the real IP address into this variable. In this case it can be used as an IP address. But of course it must be done manually, in the every particular case, not as the general recommendation. But anyway I'd quit such a webserver as there can't only be one misconfiguration in it.

要获取用户IP地址,您应该使用此地址,

$ip = $_SERVER['REMOTE_ADDR']

If your client is surfing through a proxy server, then $_SERVER['REMOTE_ADDR'] just returns the IP address of the proxy server — not of the client's machine. That's not very reliable. This might be a better solution:

function get_ip_address() {
 foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
  if (array_key_exists($key, $_SERVER)) {
   foreach (explode(',', $_SERVER[$key]) as $ip) {
    $ip = trim($ip); // just to be safe
    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
     self::$ip = $ip;
     return $ip;
    }
   }
  }
 }
}

This has already been discussed on Stack Overflow before. Please refer to “What is the most accurate way to retrieve a user's correct IP address in PHP?” . The above code is an optimized version of the accepted answer to that question.

However, do note that getting IP addresses is never fully reliable: php / ajax REMOTE_ADDR set to IP of bogus network adapter

General broad answer: everything PHP knows about client is stored in the $_SERVER variable. So, do this code everytime you want particular info to see if you can find something relevant:

echo "<pre>";
print_r($_SERVER);
echo "</pre>";
//or just
phpinfo(32);

$_SERVER['REMOTE_ADDR'] is the only IP address you can get, though it can be not a "client address".

The following would serve the purpose.

$ip = $_SERVER['REMOTE_ADDR'];

echo "IP address is : " :.$ip;

I suggest you use a third-party IP address to a geo converter to calculate the location. Since you wouldn't have to bare the database space.. Accuracy and the queries.

All you will need to do is something like

http://third-party-url.com?ip=IP_ADDRESS

and it gives back XML or an array of data.

You can use that to do whatever you want later on.

Here are two websites which are popular. * My IP Address Lookup and GeoTargeting * SimpleGeo

There are many such sites.. Maybe others can add them here.

Just use this at the top of your code:

if (function_exists('apache_request_headers'))
{
    $headers = apache_request_headers();

    if (array_key_exists('X-Forwarded-For', $headers))
    {
        $ips = explode(',',$headers['X-Forwarded-For'],2);
        $_SERVER["REMOTE_ADDR"]= trim($ips[0]);
    }
}

It will make sure that $_SERVER["REMOTE_ADDR"] always keep the proper IP address on load balancers applications or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM