<html>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<head>
<title>后台</title>
</head>
<body>
<form method="post" action="admin.php">
<table border= "1" align="center">
<tr>
<td>添加敏感词:</td><td><input type="txt" name="mingan"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="添加">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
</html>

然后新建一个admin.php

<?php
if($_POST)
{
$filename = "./index.txt";
$mingan = $_POST['mingan'];
if(is_writable("./index.txt"))
{
$file = fopen($filename,"r+");
}else{
echo "文件不可写";
}
fseek($file,0,SEEK_END);
if(fprintf($file,$mingan))
{
echo "敏感词添加成功";
}else{
echo "敏感词添加失败";
}
fprintf($file,"\r\n");
fclose($file);
}
?>

再建一个index.php这个页面用于检测敏感词 代码:

<?php
$_POST;
if($_POST)
{
$username = $_POST['username'];
$content = $_POST['content'];
if(is_file("./index.txt"))
{$file = file("./index.txt");
for($i = 0;$i<count($file);$i++)
{
if(preg_match("/".trim($file[$i])."/i",$content))
{
echo "有敏感词";
}
}
}
}
?>

最后新建一个index.txt 这个文件用于存放敏感字的单词,每次都会去这个文件匹配一下有没有敏感词,这个文件不需要写代码

转载请注明转自:运达's blog(原文地址:http://www.yunda51.com/?p=63