PHP Get User IP code implementation
Getting a client User IP is often required in the Web site, such as:
1. Login IP: You can know in time whether your account is stolen;
2. Voting: can only do one vote per IP to prevent the swipe ticket;
3. Statistics: Statistical user sources, this is an indispensable part of the data analysis;
4. Other.
So it is important to get the client IP correctly, and here is the PHP code that gets the IP in daily use:
/**
* Get customer IP Address
* @return $ip
*/
function GetIP () {
if (!empty ($_server["Http_client_ip")) {
$ip = $_server["Http_client_ip"];
}
ElseIf (!empty ($_server["Http_x_forwarded_for")) {
$ip = $_server["Http_x_forwarded_for"];
}
ElseIf (!empty ($_server["REMOTE_ADDR")) {
$ip = $_server["REMOTE_ADDR"];
}
else{
$ip = "127.0.0.1";
}
return $IP;
}
This is just to provide reference, if you have any good way to communicate with the message!