Home  >  Article  >  php教程  >  php实现禁止IP段访问网站的代码

php实现禁止IP段访问网站的代码

WBOY
WBOYOriginal
2016-05-25 16:41:011754browse

有个前提条件是我们的页面必须是php类型的页面,如果你生成了html静态页面这种方法就不可行了,下面我们来看看php实现禁止IP段访问网站的代码,代码如下:

<?php
//加IP访问限制
if (getenv(&#39;HTTP_CLIENT_IP&#39;) && strcasecmp(getenv(&#39;HTTP_CLIENT_IP&#39;) , &#39;unknown&#39;)) {
    $userip = getenv(&#39;HTTP_CLIENT_IP&#39;);
} elseif (getenv(&#39;HTTP_X_FORWARDED_FOR&#39;) && strcasecmp(getenv(&#39;HTTP_X_FORWARDED_FOR&#39;) , &#39;unknown&#39;)) {
    $userip = getenv(&#39;HTTP_X_FORWARDED_FOR&#39;);
} elseif (getenv(&#39;REMOTE_ADDR&#39;) && strcasecmp(getenv(&#39;REMOTE_ADDR&#39;) , &#39;unknown&#39;)) {
    $userip = getenv(&#39;REMOTE_ADDR&#39;);
} elseif (isset($_SERVER[&#39;REMOTE_ADDR&#39;]) && $_SERVER[&#39;REMOTE_ADDR&#39;] && strcasecmp($_SERVER[&#39;REMOTE_ADDR&#39;], &#39;unknown&#39;)) {
    $userip = $_SERVER[&#39;REMOTE_ADDR&#39;];
}
$ban_range_low = ip2long("217.0.0.0"); //ip段上
$ban_range_up = ip2long("217.255.255.255"); //ip段尾
$ip = ip2long($userip]);
if ($ip > $ban_range_low && $ip < $ban_range_up) {
    print "Banned";
    exit();
}


本文地址:

转载随意,但请附上文章地址:-)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn