PHP限IP的代码在本地试了可行但放到网上就不行了呢?

来源:百度知道 编辑:UC知道 时间:2024/07/08 02:41:11
PHP限IP的代码在本地试了可行但放到空间里就不行了呢?
PHP限IP访问的代码在本地试了可行但放到空间就不行了呢?懂的朋友说下。代码如下:

$nopass="127.0.";//定义你要禁止的IP段
$ip=getenv('REMOTE_ADDR');
if (ereg($nopass,$ip)){
echo "错误!";
exit();
}
禁止的IP是我随意设的,在本地是用花生壳访问的。禁止我当前本机的IP访问是可以的,传到空间就不行了。

在本地,你访问时得到的IP是本机内网IP或者127.0.0.1,你才会进if里边.放到空间上了,你访问时得到的IP是本机外网的IP,怎麽可能进去if里边呢?除非你用VPN连到机房里访问.

Description
string getenv ( string varname )

Returns the value of the environment variable varname, or FALSE on an error.

<?php
// Example use of getenv()
$ip = getenv('REMOTE_ADDR');

// Or simply use a Superglobal ($_SERVER or $_ENV)
$ip = $_SERVER['REMOTE_ADDR'];
?>

You can see a list of all the environmental variables by using phpinfo(). You can find out what many of them mean by taking a look at the CGI specification, specifically the page on environmental variables.