Ok, one way to do this is to check the message before it's even been added to the chat file. A easy way to accomplish this with PHP is to use PHP Sessions that will store a counter for repeated values. Since I don't know the structure of your site, I'll give you basic instructions of how to do this:
1. Start the session for the "post to chat" function
PHP Sessions need to be started wherever you use them. This can be done with a simple.
session_start();
2. Create the two session variables if it doesn't exist
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. For new links check if it matches the last link
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. If the link is a duplicate add one to the 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. Check if the duplicate_count
is larger than 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. Log the user's latest link if it is different, and reset the counter
Simply done with
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 course, you should also consider securing your sessions to prevent session hijacking
, but that is another topic that you can find plenty of answers for on Stack Overflow. This post has some good pointers: PHP Session Security