47

I am trying to fetch the ip address of my machine through php. For that I am writing the code like:

<?php echo  "<br />".$_SERVER['REMOTE_ADDR'];?>

But this piece of code is not working. It is returning "::1". Please help me how to get the actual IP Address.

hakre
  • 193,403
  • 52
  • 435
  • 836
John
  • 571
  • 2
  • 7
  • 12
  • 1
    `$_SERVER['REMOTE_ADDR']` is not the IP address of your machine. – hakre May 09 '12 at 13:45
  • Possible duplicates: [Identify server IP address with PHP](http://stackoverflow.com/questions/5800927/identify-server-ip-address-with-php) -- or if you need the other thing -- [How do I find a user's IP address with PHP?](http://stackoverflow.com/questions/55768/how-do-i-find-a-users-ip-address-with-php) – hakre May 09 '12 at 13:47
  • Of what machine? Your server that the PHP is running on? Your desktop that the browser connecting to the server is running on? – Quentin May 09 '12 at 13:47
  • 4
    ::1 is an alias for localhost – Hajo May 09 '12 at 13:47
  • 3
    `::1` is the actual IP. It is an ipv6 loopback address (i.e. localhost) – Quentin May 09 '12 at 13:47
  • if you run that php script on the server itself of course it will return ::1 (i.e. localhost). Put the script on different server. – Daniel Wu Feb 17 '21 at 04:32

6 Answers6

87

::1 is the actual IP. It is an ipv6 loopback address (i.e. localhost). If you were using ipv4 it would be 127.0.0.1.

If you want to get a different IP address, then you'll need to connect to the server through a different network interface.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
14

If you are trying to run localhost, this answer will fix your problem. Just few changes on

apache2/httpd.conf 

search all "listen" words ex:

Listen 80

Make like this.

Listen 127.0.0.1:80

than restart your apache

$_SERVER[REMOTE_ADDR]

will show Listen 127.0.0.1

you can see answer in this detailed answer link

Community
  • 1
  • 1
Yigit Tanriverdi
  • 920
  • 5
  • 19
8

If you mean getting the user's IP address, you can do something like :

<?php
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else{
      $ip=$_SERVER['REMOTE_ADDR'];
    }
?>

<?php echo  "<br />".$ip;?>

It will get the user's actual IP address, regardless of proxies etc.

Guillaume Flandre
  • 8,936
  • 8
  • 46
  • 54
4

$_SERVER['REMOTE_ADDR'] is the IP address of the client.

$_SERVER['SERVER_ADDR'] is the IP address of the server.

Reference: http://php.net/manual/en/reserved.variables.server.php

Tushar
  • 8,019
  • 31
  • 38
-1

Simple answer: You are using it on local server. Try running

function getUserIpAddr(){
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        //ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        //ip pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

echo 'User Real IP - '.getUserIpAddr();

in real server. Or you can also user online php executor.

Jack Larson
  • 184
  • 1
  • 10
-2

Look at the output of phpinfo(). If the address is not on that page, then the address is not available directly through PHP.

dotancohen
  • 30,064
  • 36
  • 138
  • 197
  • '$http_client_ip= $_SERVER['HTTP_CLIENT_IP']; $http_x_forwarded_for=$_SERVER['HTTP_X_FORWARDED_FOR']; $remote_addr=$_SERVER['REMOTE_ADDR']; if(!empty($http_client_ip)) {$ip_address=$http_client_ip;} else if(!empty($http_x_forwarded_for)) {$ip_address=$http_x_forwarded_for;} else {$ip_address=$remote_addr;} echo $ip_address;' – roger nm Aug 18 '15 at 11:33
  • 3
    Why the sudden downvote? This answer shows that the information either **is** or **is not** available to PHP. – dotancohen May 10 '16 at 10:48