IP Address of the machine in PHP gives ::1 but why

2019-01-04 11:27发布

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.

6条回答
不美不萌又怎样
2楼-- · 2019-01-04 11:37

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

查看更多
走好不送
3楼-- · 2019-01-04 11:39

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.

查看更多
爷的心禁止访问
4楼-- · 2019-01-04 11:49

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

查看更多
叼着烟拽天下
5楼-- · 2019-01-04 11:52

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.

查看更多
一夜七次
6楼-- · 2019-01-04 11:52

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

查看更多
老娘就宠你
7楼-- · 2019-01-04 12:01

$_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

查看更多
登录 后发表回答