Quick PHP code snippet: Getting visitors IP Address with PHP

Quick PHP code snippet: Getting visitors IP Address with PHP

Quick PHP code snippet: Getting visitors PHP Address with PHP

This really quick PHP code snippet enables you to retrieve the IP adress of your visitor.
Retrieving the virtual adress of your page visitor can come in really handy when you want to add extra logging to your website.

I normally keep track of the IP adresses when a contact form is submitted (or other actions are taken on the website), you never know when it might be usefull.

function getVisitorIpAddress()  
{  
    if (!emptyempty($_SERVER['HTTP_CLIENT_IP']))  
    {  
        $ip=$_SERVER['HTTP_CLIENT_IP'];  
    }  
    elseif (!emptyempty($_SERVER['HTTP_X_FORWARDED_FOR']))  
    //to check ip is pass from proxy  
    {  
        $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];  
    }  
    else  
    {  
        $ip=$_SERVER['REMOTE_ADDR'];  
    }  
    return $ip;  
}
source: webdeveloperplus

About the Author

Daan is a Creative-Geek who loves learning and sharing new techniques! Follow him on Twitter to keep up to date with the Creative-Geeks blog and other subjects. Contact him on e-mail : info[at]creative-geeks.com.