PHP中机器的IP地址给出:1但是为什么?

时间:2022-09-16 13:30:18

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

我正在尝试通过php获取我的机器的ip地址。为此,我编写的代码如下:

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

但是这段代码不起作用。这是返回“::1”。请帮助我如何得到实际的IP地址。

6 个解决方案

#1


42  

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

:1是实际的IP。它是一个ipv6环回地址(即本地主机)。如果您使用的是ipv4,那么它将是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.

如果希望获得不同的IP地址,则需要通过不同的网络接口连接到服务器。

#2


7  

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

如果您正在尝试运行localhost,这个答案将解决您的问题。只是一些改变

apache2/httpd.conf 

search all "listen" words ex:

搜索所有的“听”字例句:

Listen 80

Make like this.

使这样的。

Listen 127.0.0.1:80

than restart your apache

比重新启动apache

$_SERVER[REMOTE_ADDR]

will show Listen 127.0.0.1

将显示听127.0.0.1

you can see answer in this detailed answer link

您可以在这个详细的答案链接中看到答案。

#3


6  

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

如果您的意思是获取用户的IP地址,您可以这样做:

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

它将获得用户的实际IP地址,而不考虑代理等。

#4


3  

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

$_SERVER['REMOTE_ADDR']是客户端的IP地址。

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

$_SERVER['SERVER_ADDR']是服务器的IP地址。

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

参考:http://php.net/manual/en/reserved.variables.server.php

#5


0  

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.

在真正的服务器。也可以使用在线php执行程序。

#6


-2  

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

查看phpinfo()的输出。如果地址不在该页上,则该地址不能通过PHP直接使用。

#1


42  

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

:1是实际的IP。它是一个ipv6环回地址(即本地主机)。如果您使用的是ipv4,那么它将是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.

如果希望获得不同的IP地址,则需要通过不同的网络接口连接到服务器。

#2


7  

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

如果您正在尝试运行localhost,这个答案将解决您的问题。只是一些改变

apache2/httpd.conf 

search all "listen" words ex:

搜索所有的“听”字例句:

Listen 80

Make like this.

使这样的。

Listen 127.0.0.1:80

than restart your apache

比重新启动apache

$_SERVER[REMOTE_ADDR]

will show Listen 127.0.0.1

将显示听127.0.0.1

you can see answer in this detailed answer link

您可以在这个详细的答案链接中看到答案。

#3


6  

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

如果您的意思是获取用户的IP地址,您可以这样做:

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

它将获得用户的实际IP地址,而不考虑代理等。

#4


3  

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

$_SERVER['REMOTE_ADDR']是客户端的IP地址。

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

$_SERVER['SERVER_ADDR']是服务器的IP地址。

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

参考:http://php.net/manual/en/reserved.variables.server.php

#5


0  

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.

在真正的服务器。也可以使用在线php执行程序。

#6


-2  

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

查看phpinfo()的输出。如果地址不在该页上,则该地址不能通过PHP直接使用。