简体   繁体   中英

get the ip address of users opening my website

I want to track the IP address of the users who are opening my website. I have done lot of googling and even other questions asked on stackoverflow but couldn't find the solution. I actually want to know how to I get the IP address by using

    $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] variables. 

Use $_SERVER['REMOTE_ADDR']

if ($_SERVER['HTTP_CLIENT_IP']!="") 
{
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} 
elseif ($_SERVER['HTTP_X_FORWARDED_FOR']!="") 
{
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else 
{
    $ip = $_SERVER['REMOTE_ADDR'];
}

If your client is connected through proxy server then $_SERVER['REMOTE_ADDR'] just returns the IP address of the proxy server not of the client' machine. This is the closest you can get to client' ip.

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

Your answer is in your question: $_SERVER['REMOTE_ADDR'] will contain the IP of the user accessing your site. Just use it like you'd use any other variable.

For this the answer is simple. just use PHP's $_SERVER["REMOTE_ADDR"] to get the IP of the user who accesses a given page/url

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