How to get an IP address of a visitor to your website using PHP?

Sometimes you will want to know the IP address of any visitor to your website for marketing analysis, or for sales chart purposes, or for geo location analysis of your website visitors.  By using PHP, finding an IP address nowadays is much easier. It is very important requirement for many scripts where we store the members or visitors details. For security reason we can store IP address of our visitors who are doing any purchases or recording the geographical location of the visitor can be done this way. Sometimes based on the IP address we can redirect the browser to specific location of the site. For example, your website has advertisement banners that only targets users in San Francisco. When a visitor visit your website, you’ll record their IP address and then from there your codes will know that this visitor is in San Francisco. Therefore, an ad banner about San Francisco restaurant or hotel will be displayed on the page. There are many applications using this and below is the PHP code to recognize the IP address of any visitor of your web site.

If register_global in your php.ini is on, your codes on php page will be:

$ip=@$REMOTE_ADDR;
echo “IP Address is $ip”;

You will see this:
IP Address is 209.31.236.130

If you have a geo location by IP Lookup DB, you will find out that this sample IP address is in San Jose.

If register_global is off, your codes on php page will be:

$ip=$_SERVER[‘REMOTE_ADDR’];
echo “IP Address is $ip”;
You will see the same output as the above. With this piece of data, you can save it in your database table and redirect to an ads that targets users in this location. Or you can display a local community news for this user.

I hope that my explanation about IP address helps you in making decision for your website advertisement or your blogs.