PHP: Determine or Retrieve Visitor’s IP Address Using PHP Script

How do I determine or retrieve visitor’s IP address using PHP server side programming under Unix/Windows operating systems?

Use the following environment variables to get visitor’s IP address:
Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements PHP+LAMP
Time N/A
No Proxy detection
REMOTE_ADDR – Get the remote client IP address

With Proxy detection
HTTP_X_FORWARDED_FOR – Get the proxy server IP address.

PHP getenv() function
Use php getenv() function to read the value of the environment. The syntax is:


Example: Sample code to get an IP address

Here is the PHP code:

 <title>What is my IP address?</title>
    if (getenv('HTTP_X_FORWARDED_FOR')) {
        $pipaddress = getenv('HTTP_X_FORWARDED_FOR');
        $ipaddress = getenv('REMOTE_ADDR');
echo "Your Proxy IP address is : ".$pipaddress. "(via $ipaddress)" ;
    } else {
        $ipaddress = getenv('REMOTE_ADDR');
        echo "Your IP address is : $ipaddress";

If GEOIP database installed and configured using Apache/Nginx/Lighttpd, try the following php code to get the visitors country name:

   $country = getenv('GEOIP_COUNTRY_NAME');
   echo "Your country : $country";

You can try the demo by visiting this url.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 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
35 comments… add one
  • AlAa YoSeF Mar 21, 2012 @ 15:32

    Hi every one my name is AlAa

    My problem with $_SERVER[‘remote_addr’]

    I’m not sure if I’m having a problem with the installation of PHP on the server or if there’s something that I’m not doing right in the code, but I am trying to capture the IP address of anyone that is trying to log into a secure part of a site. Regardless of my IP address, when I do:


    echo $_SERVER[‘remote_addr’];

    It returns ‘’. This is the first time I’ve written code that is being used on a Windows Server, so I don’t know if that has anything to do with it.

    I’ve run phpinfo(), and in the PHP Variables section, it shows “_SERVER[“REMOTE_ADDR”]” : Does this mean that the php.ini file is setup wrong, or am I missing something?

    i am running my own server on windows 7 iis7.5 with php as fastcgi and i am behind a router with a dynamic ip not static and i am using that give me a link to my router and i am opening 80 port in my router forwarding it to my id address and i have tested a php file that contains

    tested it using tor network that give me ip out of my network to test that file always give me the same result i am trying to get IP address of the visitor and i asked someone of my friends to use this form it sends the same ip

    i hope someone tells me what i am doing wrong and the solution for this problem

    Thanks in advance

  • toodle bumpleskin May 11, 2012 @ 16:22

    This is a good link, fixed the same error for me when I went through it!

  • PhpDev Sep 27, 2012 @ 23:38


    It’s a great thing what you have given here… It solved my problem on Server.

    Again thank you very much for this.

    Thanks & Regards,

  • Ganesh Feb 9, 2013 @ 4:25

    i want add in my blogger , blogspot site, please help me your code php not work with blogspot any other idea please give me, i want see my visitors ip..!

  • Mahi Sep 12, 2014 @ 7:28

    Am using this code but i am getting only last part of ip address( : : :1).how can i get full ip address..if any one know answer please reply.

  • Mi ip Dec 17, 2014 @ 11:51

    hi! Thanks for share!!! It,s work for me. How can i make a my ip widget ?
    Thanks again!

  • Naushad Ali Dec 22, 2014 @ 16:07


  • seema Feb 4, 2015 @ 20:30


    Iam using same code but its displaying Your IP address is : ::1 .Please help me.

    • srinu Jul 13, 2015 @ 0:34

      u r seeing in server comp so this way it will displays loopback address::
      check it in other computer you will see the ip .

  • Shubham Rana Oct 16, 2015 @ 10:19

    It’s a Nice and Simple Code…

  • zuber Oct 17, 2015 @ 9:45

    create a php page which show the ip address of client who is visting the website , on page load, alert my id address ,

    so plz help me dear sir plz help this problume

  • rufo Feb 3, 2016 @ 3:10

    nah. this gives ip address of the proxy server. Zzzz

Leave a Reply

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

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