我有一些聊天机器人脚本,我需要一种方法来阻止那些在聊天中转发同一链接超过2次的用户的垃圾邮件。
$a = file_get_contents($url);
$matches = explode('<tr id=', $a);
for($i=2; $i<15; $i++) {
$mess = $matches[$i];
preg_match('%"(.*)">%U', $mess, $id);
$id_user = $id[1];
preg_match('%<b class="(.*)">(.*)</b>%U', $mess, $mem);
$name = $mem[2];
preg_match('%</b>:(.*)</td></tr>%U', $mess, $chat);
$chat = $chat[1];
$link = explode('<a href="', $chat);
$link = explode('"', $link[1]);
$link = $link[0];
现在,我需要的是一个函数来计算同一个链接是否由同一个用户名发布了2次并发出警告,以及是否调用了3次另一个函数以禁止我所拥有的功能。
好的,一种方法是在消息添加到聊天文件之前检查它。用PHP实现这一点的一个简单方法是使用PHP会话,该会话将存储重复值的计数器。由于我不知道你的网站结构,我会给你如何做到这一点的基本说明:
1.启动"发布聊天"功能的会话
PHP会话需要在任何使用它们的地方启动。这可以用一个简单的
session_start();
2.如果不存在,则创建两个会话变量
session_start();
if(!isset($_SESSION['latest_link'])){ //isset() checks whether or not the variable exists
$_SESSION['latest_link'] = "";
}
if(!isset($_SESSION['duplicate_count'])){
$_SESSION['duplicate_count'] = 0;
}
3.对于新链接,请检查它是否与最后一个链接匹配
session_start();
if(!isset($_SESSION['latest_link'])){ //isset() checks whether or not the variable exists
$_SESSION['latest_link'] = "";
}
if(!isset($_SESSION['duplicate_count'])){
$_SESSION['duplicate_count'] = 0;
}
if($_SESSION['latest_link'] == trim(strtolower($link))){ //where $link is the new link being posted. trim() removes unneeded whitespace and strtolower() makes everything lowercase. This way, the poster can't fool the system through capitalization or extra spaces
}
5.如果链接重复,则在duplicate_count
中添加一个
session_start();
if(!isset($_SESSION['latest_link'])){ //isset() checks whether or not the variable exists
$_SESSION['latest_link'] = "";
}
if(!isset($_SESSION['duplicate_count'])){
$_SESSION['duplicate_count'] = 0;
}
if($_SESSION['latest_link'] == trim(strtolower($link))){ //where $link is the new link being posted. trim() removes unneeded whitespace and strtolower() makes everything lowercase. This way, the poster can't fool the system through capitalization or extra spaces
$_SESSION['duplicate_count']++; //add one to duplicate_count
}
6.检查duplicate_count
是否大于2
session_start();
if(!isset($_SESSION['latest_link'])){ //isset() checks whether or not the variable exists
$_SESSION['latest_link'] = "";
}
if(!isset($_SESSION['duplicate_count'])){
$_SESSION['duplicate_count'] = 0;
}
if($_SESSION['latest_link'] == trim(strtolower($link))){ //where $link is the new link being posted. trim() removes unneeded whitespace and strtolower() makes everything lowercase. This way, the poster can't fool the system through capitalization or extra spaces
$_SESSION['duplicate_count']++; //add one to duplicate_count
}
if($_SESSION['duplicate_count'] > 2){
//user has posted same link more than 2 times. Action should be taken.
}
7.如果用户的最新链接不同,请记录该链接,并重置计数器
只需
session_start();
if(!isset($_SESSION['latest_link'])){ //isset() checks whether or not the variable exists
$_SESSION['latest_link'] = "";
}
if(!isset($_SESSION['duplicate_count'])){
$_SESSION['duplicate_count'] = 0;
}
if($_SESSION['latest_link'] == trim(strtolower($link))){ //where $link is the new link being posted. trim() removes unneeded whitespace and strtolower() makes everything lowercase. This way, the poster can't fool the system through capitalization or extra spaces
$_SESSION['duplicate_count']++; //add one to duplicate_count
}else{
$_SESSION['latest_link'] = trim(strtolower($link));
$_SESSION['duplicate_count'] = 0;
}
if($_SESSION['duplicate_count'] > 2){
//user has posted same link more than 2 times. Action should be taken.
}
当然,您也应该考虑保护会话以防止session hijacking
,但这是另一个可以在Stack Overflow上找到很多答案的主题。这篇文章有一些很好的指针:PHP会话安全