如何使用php或php会话计算聊天页面中的重复帖子?

如何使用php或php会话计算聊天页面中的重复帖子?,php,count,explode,spam-prevention,Php,Count,Explode,Spam Prevention,我有一些聊天机器人脚本,我需要一种方法来阻止垃圾邮件的用户谁保持在聊天相同的链接超过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]; pre

我有一些聊天机器人脚本,我需要一种方法来阻止垃圾邮件的用户谁保持在聊天相同的链接超过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];
$a=文件获取内容($url);
$matches=分解('%U',$mess,$id);
$id_user=$id[1];
预匹配('%(.]%U',$mess,$mem);
$name=$mem[2];
预匹配('%:(.*)%U',$mess,$chat);
$chat=$chat[1];

$link=explode(“好的,一种方法是在消息被添加到聊天文件之前检查消息。使用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.如果链接重复,则在
重复计数中添加一个链接

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.检查重复计数是否大于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.
}

当然,你也应该考虑确保你的会话以防止<代码>会话劫持< /代码>,但是这是另一个你可以为堆栈溢出找到答案的话题。这个帖子有一些好的指针:

什么是问题?你尝试了什么?你可以通过跟踪PHP的最后三条消息来轻松地做到这一点。但是,我似乎不理解上下文和您到目前为止所尝试的内容。@PatrickJamesMcDougle,我有点不懂编码,但我尝试了
count(explode
问题是我不知道如何让脚本计算同一用户的确切聊天词或链接
$name
,而不是所有人users@KevinPei,你能解释一下吗,因为我是个疯子:)我认为,您需要在某种持久性存储中跟踪这一点。如果问题是防止垃圾邮件,有没有办法防止垃圾邮件发送者轻松获取帐户?验证码或基本图灵测试?非常感谢,明天我将尝试一下,因为它已经是世界的一部分:)再次感谢Hello,很抱歉我的回复太迟了,因为我一直很忙。我正准备测试你的代码,然后我注意到代码没有通过相同的
$name
注册重新发布的链接?它只是检查重新发布的链接一般情况下你好,我设法让它工作,但有办法让它在5分钟内计数重新发布的链接吗?!没有所有帖子?提前谢谢还有一件事,plz,它现在确实检测到链接和聊天,但它检测到所有链接,而不是转发的相同链接:)请帮助我,谢谢