2013-12-16 78 views
0

我有一些聊天機器人腳本,我需要一種方法來阻止不斷重複發佈相同鏈接的用戶在兩次以上的聊天中發送垃圾郵件。如何使用php或php會話在聊天頁面中重複發帖?

$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次調用另一個函數來禁止這一點我有。

+0

什麼似乎是問題?你有什麼嘗試? –

+0

您可以通過使用PHP會話跟蹤最後三條消息輕鬆完成此操作。但是,我似乎並不瞭解上下文以及迄今爲止嘗試的內容。 –

+0

@PatrickJamesMcDougle,我是種類的noob與編碼,但我試過'計數(爆炸)的問題是我不知道如何使腳本計算確切的聊天單詞或鏈接由同一用戶'$ name'而不是所有用戶 – GuestofHonor

回答

1

好的,一種方法是在消息被添加到聊天文件之前檢查消息。使用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. 
} 

Of co你也應該考慮保護你的會話以防止session hijacking,但這是另一個話題,你可以在Stack Overflow上找到很多答案。這篇文章有一些好的指點:PHP Session Security

+1

非常感謝,我會試試看的明天,因爲它已經是凌晨4點這個世界的一部分:) 再次感謝 – GuestofHonor

+0

你好,對不起 後期的答覆,因爲我一直很忙。我現在要測試你的代碼,然後我注意到代碼沒有通過相同的'$ name'註冊轉貼的鏈接?它只是檢查重新發布的鏈接一般 – GuestofHonor

+0

現在它只讀聊天,而不是鏈接! – GuestofHonor