Getting the IP address using PHP

You can display the visitor's IP address on a webpage or you can use it for other purposes such as storing it for security (if you're running an e-commerce site), redirecting the visitor, even banning the visitor from your website if you need to.

Get IP address of a visitor

Use the REMOTE_ADDR index of the $_SERVER array to get the IP address of the visitor.

Example:
Your IP address is <?php echo $_SERVER['REMOTE_ADDR']; ?>
Output:
Your IP address is 54.226.113.250

Get IP address of a website

You can get the IP address of a website by it's URL. Include the URL as a parameter of the gethostbyname() function.

Example:
IP address of google.com is <?php echo gethostbyname('www.google.com'); ?>
Output:
IP address of google.com is 216.58.192.164
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information