How to get visitors IP address in PHP

Hi, This is very simple thing, but might helpful for beginners. So bellow are the different codes for getting visitors IP address.

//Globals off work round
if (!ini_get('register_globals')) {
    $reg_globals = array($_POST, $_GET, $_FILES, $_ENV, $_SERVER, $_COOKIE);
    if (isset($_SESSION)) {
        array_unshift($reg_globals, $_SESSION);
    }
    foreach ($reg_globals as $reg_global) {
        extract($reg_global, EXTR_SKIP);
    }
}

//Find the IP of the visitor

if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
    $rip = getenv("HTTP_CLIENT_IP");
} else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
    $rip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
    $rip = getenv("REMOTE_ADDR");
} else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
    $rip = $_SERVER['REMOTE_ADDR'];
} else {
    $rip = "unknown";
}

//Display the IP of the Visitor

echo "Your IP is $ip";

If there any confusion occurs then feel free to contact me..

Advertisements

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s