软件教程导航   > 电脑书籍 > PHP > 基于PHP的聊天室(三)
 
基于PHP的聊天室(三)

来源: limodou
作者: Mike Hall
译者: limodou

   现在我们有了需要通过$REMOTE_ADDR变量来交叉引用的文件,这样我们可以区分出想要发贴的用户是否已经被扁或没有被扁。很简单:

< ?php

for ($counter=0;$counter< sizeof($banned_array);$counter++) {
if ($banned_array[$counter] == $REMOTE_ADDR) {
print("< font color="red" face="arial" align="center" >".
"You have been banned from this chat< /font >");
exit;
}
}

? >

   exit命令将立即停止脚本的执行。在开始对传递过来的变量执行处理之前,插入对被扁用户的检查,这样被扁用户就不能使用聊天室了。

   比较好的解决在某些情况下动态IP地址的问题的一个意见就是,检查IP地址块的所属范围。一个简单的函数可以容易地实现它。

< ?php

function makeMask($ip) {
// remember to escape the . so PHP doesn't think it's a concatenation
$ip_array = explode(".", $ip);
$ip_mask = "$ip_array[0].$ip_array[1].$ip_array[2]";
return $ip_mask;
}

? >

   然后我们把循环中的if替换成

< ?php

for ($counter=0;$counter< sizeof($banned_array);$counter++) {
if (makeMask($REMOTE_ADDR) == makeMask($banned_array[$counter])) {
print("< font color="red" face="arial" align="center" >".
"You have been banned from this chat< /font >");
exit;
}
}

? >

   我们有了针对动态IP地址的保护措施。
 
下一页