PHP: HowTo Read an IP Address of Remote Computer / Browser

How do I read an IP address of remove system or browser using PHP server side programming language? How can I get client IP address and store it to the variable called $ip using php+apache?

PHP has in built facility to detect remote browser, IP address, and other properties. These values are assigned to special variable. In this case, it is assigned to a variable called REMOTE_ADDR. You can use any one of the following statement to obtained or read IP address.

PHP syntax to find out an IP address

The syntax is:







$ip = getenv('HTTP_CLIENT_IP');

PHP syntax to find out remote host name

The syntax is:

$hostname = gethostbyaddr($ip);


$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);


Create a file called client-ip.php and store or upload at your Web-server DocumentRoot (e.g. /var/www/html/client-ip.php):

// client-ip.php : Demo script by SXI LLC <>
// get an IP address
// display it back
echo "<h2>Client IP Demo</h2>";
echo "Your IP address : " . $ip;
echo "<br>Your hostname : ". gethostbyaddr($ip) ;

Save and close the file. Open a web-browser and type your url:
Sample outputs:

Fig.01 client-ip.php output

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

How to Make Website WCAG Compliant?

Next Post

Link download Kali Linux 2020.1 (ISO + Torrent)

Related Posts