简体   繁体   English

我的IP在PHP家庭服务器中显示错误

[英]My IP is showing up wrong in PHP home server

Ok simple enough 好的很简单

<?PHP
echo $_SERVER[REMOTE_ADDR];
?>

Ok maybe not, I my IP is currently 72.184.212.85 however the code above which I am using on an IP blocking system for a script shows my IP as my home server IP of 127.0.0.1 好吧也许不是,我的IP目前是72.184.212.85但是我在脚本的IP阻塞系统上使用的代码显示我的IP作为我的家庭服务器IP 127.0.0.1

So when I go to my script my IP is shown as 127.0.0.1 but when I go to other websites it is shown as 72.184.212.85 因此,当我转到我的脚本时,我的IP显示为127.0.0.1但是当我转到其他网站时,它显示为72.184.212.85

How can I get the first value to show on my test server? 如何在测试服务器上显示第一个值?

$_SERVER['REMOTE_ADDR'] will always show the IP address from which the request came. $_SERVER['REMOTE_ADDR']将始终显示请求来自的IP地址。 If you access your own script on your own computer within your own network, your external IP address never comes into play. 如果您在自己的网络中在自己的计算机上访问自己的脚本,则外部IP地址永远不会发挥作用。 The request would have to leave your local network and then come back in for the external address to show up, but if it's all local, that'll never happen. 该请求必须离开您的本地网络,然后返回以显示外部地址,但如果它全部是本地的,则永远不会发生。

You'll have to make your server publicly accessible and then access it from the public address. 您必须使您的服务器可公开访问,然后从公共地址访问它。 I'm guessing you're currently using localhost to access your server? 我猜你正在使用localhost来访问你的服务器?

run your server say port 8080 and then forward the port in your router so it's public to the internet. 运行你的服务器说端口8080,然后转发你的路由器中的端口,以便它是公共的互联网。 Then visit your webpage/phpscript from http://72.184.212.85:8080 instead of http://localhost:8080 . 然后,从浏览网页/ phpscript http://72.184.212.85:8080而不是HTTP://本地主机:8080

Here is a ridiculous solution that I wouldn't recommend: 这是一个荒谬的解决方案,我不建议:

Register your home IP with a domain name, then see where the request came from via URL: 使用域名注册您的家庭IP,然后通过URL查看请求的来源:

$url = $_SERVER["SERVER_NAME"];

or 要么

$url = $_SERVER["HTTP_HOST"];

and then do a dns lookup of that result, which should return the IP it's registered to, ie your external IP. 然后对该结果执行dns查找,该结果应返回其注册的IP,即您的外部IP。

 $ext_ip = gethostbyaddr($url);

The only reason this wouldn't work (so sorry if I'm wrong), is if SERVER_NAME uses the same method as "REMOTE_HOST", which is a reverse DNS lookup, which won't resolve, as your internal IP won't be registered to that domain name. 唯一的原因是这不起作用(如果我错了,那就很抱歉)是,如果SERVER_NAME使用与“REMOTE_HOST”相同的方法,这是一种反向DNS查找,无法解析,因为您的内部IP不会注册到该域名。 An easy way to check is to do either: 一种简单的检查方法是:

 phpinfo();

and see what the environmental variables are. 并看看环境变量是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM