PHP 警告:file_get_contents(http://ipecho.net/plain):无法打开流:连接超时
PHP Warning: file_get_contents(http://ipecho.net/plain): failed to open stream: Connection timed out in
我这里出错了,不知道具体问题出在哪里。
我正在尝试制作一个 PHP 文件并拒绝任何人使用该脚本,除了我在代码中输入的 IP:
$pass = '192.168.x.x'; //here is the ip of the client
if (file_get_contents('http://ipecho.net/plain') !== $pass) {
我使用代码时遇到问题
PHP Warning: file_get_contents(http://ipecho.net/plain): failed to open stream: Connection timed out
为什么要用那个网站来获取客户端的 IP?
您可以使用此 PHP 函数获取客户端的 IP 地址。
只要应用程序在代理后面(例如 Cloudflare):
function get_client_ip() {
$ipaddress = '';
if ($_SERVER['HTTP_CLIENT_IP'])
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if($_SERVER['HTTP_X_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if($_SERVER['HTTP_X_FORWARDED'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if($_SERVER['HTTP_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if($_SERVER['HTTP_FORWARDED'])
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if($_SERVER['REMOTE_ADDR'])
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
当应用程序不在代理后面时:
function get_client_ip() {
return $_SERVER['REMOTE_ADDR'];
}
最终代码应该是这样的:
$pass = '192.168.x.x'; //here is the ip of the client
if (strcmp(get_client_ip(),$pass) == 0 ) {
if (file_get_contents('http://ipecho.net/plain') !== $pass)
请注意,使用此代码,您比较的是您服务器的 ip 和 pass 而不是您的客户端!
所以你的代码是错误的。另一个问题是使用 '!==',它应该只用于布尔类型而不是字符串
所以快速修复是:
if ($_SERVER['REMOTE_ADDR'] != $pass)
我这里出错了,不知道具体问题出在哪里。 我正在尝试制作一个 PHP 文件并拒绝任何人使用该脚本,除了我在代码中输入的 IP:
$pass = '192.168.x.x'; //here is the ip of the client
if (file_get_contents('http://ipecho.net/plain') !== $pass) {
我使用代码时遇到问题
PHP Warning: file_get_contents(http://ipecho.net/plain): failed to open stream: Connection timed out
为什么要用那个网站来获取客户端的 IP?
您可以使用此 PHP 函数获取客户端的 IP 地址。
只要应用程序在代理后面(例如 Cloudflare):
function get_client_ip() {
$ipaddress = '';
if ($_SERVER['HTTP_CLIENT_IP'])
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if($_SERVER['HTTP_X_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if($_SERVER['HTTP_X_FORWARDED'])
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if($_SERVER['HTTP_FORWARDED_FOR'])
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if($_SERVER['HTTP_FORWARDED'])
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if($_SERVER['REMOTE_ADDR'])
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
当应用程序不在代理后面时:
function get_client_ip() {
return $_SERVER['REMOTE_ADDR'];
}
最终代码应该是这样的:
$pass = '192.168.x.x'; //here is the ip of the client
if (strcmp(get_client_ip(),$pass) == 0 ) {
if (file_get_contents('http://ipecho.net/plain') !== $pass)
请注意,使用此代码,您比较的是您服务器的 ip 和 pass 而不是您的客户端!
所以你的代码是错误的。另一个问题是使用 '!==',它应该只用于布尔类型而不是字符串 所以快速修复是:
if ($_SERVER['REMOTE_ADDR'] != $pass)