Home > php教程 > php手册 > php实现封IP功能

php实现封IP功能

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-25 16:51:46
Original
1324 people have browsed it

早起一起突然看到,我的网站有人在恶心乱搞,为了防止这种事情我想了一个办法那就是封用户的IP了.首页我们来建数据库:

CREATE TABLE `su_lockip` ( 
  `id` int(4) NOT NULL auto_increment, 
  `lockip` varchar(1024) default NULL, 
  PRIMARY KEY  (`id`) 
)
Copy after login

下页来创建一个封文件的页面,主要是用户写入IP以"|"分开,这个页面就不多写了,我就简单的写一下,入库代码$UlockIp=$_POST['z']?$_POST['z']:'';

<?php
if (empty($UlockIp)) {
    exit("<script>alert(&#39;对不起,你输入的信息有误!&#39;);history.back();</script>");
}
$sql = "update su_lockip set lockip=&#39;$UlockIp&#39;";
if (mysql_query($sql)) {
    exit("<script>alert(&#39;锁定成功!&#39;);history.back();</script>");
} else {
    exit("<script>alert(&#39;对不起,你输入的信息有误!&#39;);history.back();</script>");
}
?>
Copy after login

就这么简单,最后就是进行锁定的了.下面代码是根据数据中是否存用户IP,如果在就提示被KILL了.代码如下:

<?php
function lock_user_ip() {
    $Usql = mysql_query("select * from su_lockip");
    $Urs = mysql_fetch_array($Usql);
    $UlockIp = $Urs[&#39;lockip&#39;];
    $ClockIp = $this->get_real_ip();
    $Iplist = explode(&#39;|&#39;, $UlockIp);
    if (in_array($ClockIp, $Iplist)) {
        exit(&#39;sorry system lock your IP&#39;);
    }
}
function get_real_ip() { //这段代码来是互联网.
    $ip = false;
    if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    }
    if (!empty($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;])) {
        $ips = explode(", ", $_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]);
        if ($ip) {
            array_unshift($ips, $ip);
            $ip = FALSE;
        }
        for ($i = 0; $i < count($ips); $i++) {
            if (!eregi("^(10|172.16|192.168).", $ips[$i])) {
                $ip = $ips[$i];
                break;
            }
        }
    }
    return ($ip ? $ip : $_SERVER[&#39;REMOTE_ADDR&#39;]);
}
?>
Copy after login

哈哈写完了就这么简单,没有进行安全过滤处理.

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template