好的,一種方法是在消息被添加到聊天文件之前檢查消息。使用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
什麼似乎是問題?你有什麼嘗試? –
您可以通過使用PHP會話跟蹤最後三條消息輕鬆完成此操作。但是,我似乎並不瞭解上下文以及迄今爲止嘗試的內容。 –
@PatrickJamesMcDougle,我是種類的noob與編碼,但我試過'計數(爆炸)的問題是我不知道如何使腳本計算確切的聊天單詞或鏈接由同一用戶'$ name'而不是所有用戶 – GuestofHonor