限制IP进入某些页面的方法
时间:2009-05-05 来源:yqh860921
黑客常常在访问你的页面时,不断的在检查你的网站是否有漏洞,其中不免有人会在浏览器里敲入STRING来判断有无SQL注入漏洞,我想了一个办法就是封住他的IP,第一次你可以访问,但是你做了不好的事,呵呵,不让他访问.
<?php
function getip() {
if (isset($_SERVER)) {
if (isset($_SERVER[HTTP_X_FORWARDED_FOR])) {
$realip = $_SERVER[HTTP_X_FORWARDED_FOR];
} elseif (isset($_SERVER[HTTP_CLIENT_IP])) {
$realip = $_SERVER[HTTP_CLIENT_IP];
} else {
$realip = $_SERVER[REMOTE_ADDR];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")) {
$realip = getenv( "HTTP_X_FORWARDED_FOR");
} elseif (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
function banip(){
$banip=@file('banip.txt');
if(!$banip) return ;
if(in_array(getip(),$banip)){
header('HTTP/1.1 404 Not Found');
?>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>404 Not Found</TITLE>
</HEAD><BODY>
<H1>Not Found</H1>
The requested URL <?php echo dirname($_SERVER["scrīpt_NAME"]);?> was not found on this server.<P>
<HR>
<ADDRESS>Web Server at <?php echo $_SERVER["SERVER_NAME"];?> Port <?php echo $_SERVER["SERVER_PORT"];?></ADDRESS>
</BODY></HTML>
<?php
die();
}
}
banip();
echo '可以访问';
?>
同级目录放个banip.txt
譬如
127.0.0.1
192.168.0.1