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?

Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements PHP
Time 5m
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.

ADVERTISEMENTS

PHP syntax to find out an IP address

The syntax is:

$ip = $_SERVER['REMOTE_ADDR'];

OR

$ip= $REMOTE_ADDR;

OR

$ip = $_SERVER['REMOTE_ADDR'];

OR

$ip = getenv('HTTP_CLIENT_IP');

PHP syntax to find out remote host name

The syntax is:

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

OR

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

Example

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

<?php
// client-ip.php : Demo script by nixCraft <www.cyberciti.biz>
// get an IP address
$ip = $_SERVER['REMOTE_ADDR'];
// 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:
http://your-sercer-name-here/client-ip.php
OR
http://127.0.0.1/client-ip.php
Sample outputs:

Fig.01 client-ip.php output

Fig.01 client-ip.php output

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
29 comments… add one
  • soroccoheaven Oct 2, 2007 @ 15:49

    hi,

    I am trying to figure out the “spam” word enabled form submission like this one …anyone here can help me ..

    Thanks

  • Dmitri Zhuchkov Nov 28, 2007 @ 15:30

    echo “ Your hostname : ” . GetHostByName($ip);

    You can get the host name from IP.gethostbyname

    gethostbyname – Get the IP address corresponding to a given Internet host name

    Try the gethostbyaddr for this task.

  • Rajesh Jan 2, 2008 @ 13:23

    How to get the remote IP location?

  • Timmehx Jan 4, 2008 @ 17:42

    <?php
    $ip = $_SERVER[‘REMOTE_ADDR’];

    And if you want to echo it use

    that is what i use

  • Nilesh Jan 6, 2008 @ 2:09

    hey! this is a child’s play! such an easy thing! i said that coz i am 15 and a hard core PHP developer!!

    • Nathan Aug 25, 2013 @ 2:12

      I’m sure you are, but not everyone is.

  • Ben Dec 11, 2008 @ 15:40

    I don’t understand something. 5 of my friends using different HOSTS in different CITIES used a script that logged each user’s IP address and time in a database and all their IP was exactly the same as mine. I used $_SERVER[‘REMOTE_ADDR’]

    • Joshua Apr 22, 2011 @ 22:12

      Hey ben, all of your IP’s are the same since all of you are using the same ISP or the same main ISP:

      user1->ISP1->main_isp->internet
      you->ISP3->main_isp->internet

      So the website actually detects your main_isp IP address since none of you have a dedicated IP address on the internet.

  • Geert Mar 22, 2009 @ 13:38

    They are all behind the same proxy-server. In that case $_SERVER[‘REMOTE_ADDR’] provides you with the ip-address of the proxy-server.

    This function will determine the real ip-adress:

    function ipCheck() {
    		if (getenv('HTTP_CLIENT_IP')) {
    			$ip = getenv('HTTP_CLIENT_IP');
    		}
    		elseif (getenv('HTTP_X_FORWARDED_FOR')) {
    			$ip = getenv('HTTP_X_FORWARDED_FOR');
    		}
    		elseif (getenv('HTTP_X_FORWARDED')) {
    			$ip = getenv('HTTP_X_FORWARDED');
    		}
    		elseif (getenv('HTTP_FORWARDED_FOR')) {
    			$ip = getenv('HTTP_FORWARDED_FOR');
    		}
    		elseif (getenv('HTTP_FORWARDED')) {
    			$ip = getenv('HTTP_FORWARDED');
    		}
    		else {
    			$ip = $_SERVER['REMOTE_ADDR'];
    		}
    		return $ip;
    	}
    • Sarmad Mahar Jun 17, 2010 @ 4:47

      it do not work if client computer is using ISA Server.

      but code is working fine if client is connected with DSL , or modem direct internet connection.

      can any one provide how do I resolved issue on intranet application/ Client is using ISA

  • leni Jun 10, 2009 @ 17:47

    how can i get the ip address of the client requesting a particular url?
    if possible give me a php code for the same..

  • Young Bobby Oct 22, 2009 @ 15:21

    Yay;

    Thanks it’s working fine !

  • chuman Sep 24, 2010 @ 18:53

    supose am chatting with my friend using gtalk how would i get his ip address. [what command do i got to use in DOS]. what i got his ip what command [DOS} do i use to get his host name.
    One more thing to what command to view that a specified computer is online or not.

  • DarkCoder Nov 29, 2010 @ 15:10

    Seems not to work for ‘localhost’ but works well with other IPs in the system

  • Marko Apr 12, 2011 @ 9:01

    Why do you have 2 x same conditions in this script ?

    elseif (getenv(‘HTTP_X_FORWARDED_FOR’)) {
    $ip = getenv(‘HTTP_X_FORWARDED_FOR’); x 2
    }
    and

    elseif (getenv(‘HTTP_FORWARDED’)) {
    $ip = getenv(‘HTTP_FORWARDED’); x 2
    }

    • Joshua Apr 22, 2011 @ 22:49

      they arent 2 same conditions in the script marco, if you check back you will notice all the conditions have a slight difference

  • Magson Fernandes Jul 15, 2011 @ 23:18

    Can any post a simple ip detection script or code. i need it please.

  • MoCua.Com Aug 6, 2011 @ 20:46

    thanks and thanks

  • CodeSlayer Sep 1, 2011 @ 0:18

    Simple working excellent

    <?php
    $ip = $_SERVER['REMOTE_ADDR'];
    $hostaddress = gethostbyaddr($ip);
    $browser = $_SERVER['HTTP_USER_AGENT'];
    $referred = $_SERVER['HTTP_REFERER']; // a quirky spelling mistake that stuck in php
    
    print "Display IP address:";
    print "$ip";
    print "More detailed host address:";
    print "$hostaddress";
    print "Display browser info:";
    print "$browser";
    print "Where you came from (if you clicked on a link to get here:";
    if ($referred == "") {
    print "Page was directly requested";
    }
    else {
    print "$referred";
    }
    ?>
    
  • anand Sep 14, 2011 @ 5:22

    how to get ip with computer name in php

  • developer rashtra Sep 23, 2011 @ 7:28

    thanks for the code. Looking for php code to get computer name of a visitor.

  • lineesh Jan 20, 2012 @ 6:43

    thanks vivek

  • ram Jan 30, 2012 @ 16:33

    how to send messages or popups to remote ip address

  • Jansha Dec 5, 2013 @ 7:15

    Very useful. Thanks for sharing

  • Zane Apr 17, 2014 @ 15:31

    Great article, thank you it’s really useful for me because i want to track how many users logged into my website daily.

  • dhananjay Oct 20, 2014 @ 9:21

    hii..
    anyone tell me how to detect the mysql ip address of remote database..

  • tejal Apr 15, 2016 @ 11:28

    How to get the default gateway ip address????????????

  • bhargvi Mar 12, 2017 @ 17:24

    ya.. He is write. u r hard core developer bt everyone is not.

  • boy Jun 1, 2017 @ 2:05

    Hi
    $hostname = gethostbyaddr($ip); very slow.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.