How to capture a users IP address

Step 1 – Get visitors IP address with PHP
Getting a visitors IP address can be important for a lot of reasons, for example, logging, geo targeting, redirecting the user and so on. All of the IP relevant informations can be found in the $_SERVER array. The simplest way to get the visitors IP address is as simple as the following code, by reading the REMOTE_ADDR field:
$ip = $_SERVER['REMOTE_ADDR'];
PHP F1
However this solution is not completely accurate, as if the user sits behind a proxy, then you will get the IP of the proxy server and not the real user address. Fortunately we can make some additional refinement to get more accurate results. Proxy servers extend the HTTP header with new property which stores the original IP. The name of this filed is X-Forwarded-For or Client-Ip. If one of these fields are present in the HTTP header then you can read their values from the $_SERVER array as in the first example. So you need to check all the 3 possibilities:
echo "Remote addr: " . $_SERVER['REMOTE_ADDR']."
";
echo "X Forward: " . $_SERVER['HTTP_X_FORWARDED_FOR']."
";
echo "Clien IP: " . $_SERVER['HTTP_CLIENT_IP']."
";
Using this information is quite easy now to create a simple function which returns the probably real ip of the site visitor:
function getIp() {
    $ip = $_SERVER['REMOTE_ADDR'];
 
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
 
    return $ip;
}
You successfully traced an email address legally. Hope you enjoyed this article. Don’t forget to share this article with friends. Visit SOG for more awesome articles like this.
SHARE

About We Greenz

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment