How to get Visitor's IP Address using PHP

Getting the IP address of your Visitor is possible using PHP. Finding the IP address is very important requirement for many scripts where we store the members or visitors details. For security reasons we can store IP address of our visitors who are doing any purchases or recording the geographical location. Sometimes, basing on a particular IP address, we can redirect the browser to different areas of the site. There are many applications using this actually.

Here's the PHP code to know the IP address of a guest visiting the site.

$ip = @$REMOTE_ADDR;
echo "IP Address = $ip";

The above code will display this IP address= 121.96.220.61

Tip: The @(at) sign suppresses any errors encountered by the Variable, Function, or Method it preceded.


What if register_global is turned Off?

In php.ini, if register_global is set to Off state, then we have to change the script a bit to get the address. Here it is:

$ip = $_SERVER['REMOTE_ADDR'];




Getting IP Addresses in CodeIgniter

Here's the code that you need to place in your script:

$this->input->ip_address()



Bookmark and Share

0 comments :

Post a Comment

Hi there! Please leave your message here. Also, I may not be able to respond to your query right away. So please bear with me. Thanks. :)