基于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地址的保护措施。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器