0

I have some chat bot script and I need a way to stop spam of users who keep reposting same link in chat more than 2 times.

$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];

Now what I need is for a function to count if the same link was posted 2 times by the same user $name and give a warning and if 3 times to call another function to ban which I do have.

GuestofHonor
  • 125
  • 4
  • 16
  • What seems to be the problem? What have you tried? – Patrick James McDougle Dec 16 '13 at 23:32
  • You can easily do this by tracking the last three messages with PHP Sessions. However, I don't seem to understand the context and what you've tried so far. – Kevin Pei Dec 16 '13 at 23:33
  • @PatrickJamesMcDougle, i'm kind of noob with coding but i tried `count(explode` problem is i dont know how to make the script calculate exact chat words or links by the same user `$name` and not by all users – GuestofHonor Dec 16 '13 at 23:33
  • @KevinPei, can you please explain since i'm a noobie :) – GuestofHonor Dec 16 '13 at 23:36
  • You are going to need to keep track of that in some sort of persistent storage I think. If the problem is spam-prevention, is there a way to keep the spammers from getting accounts easily? Captchas or basic turing tests? – Patrick James McDougle Dec 16 '13 at 23:37
  • @PatrickJamesMcDougle, it's not that easy since spammers are not bots and they can bypass captcha and promote other sites url's easily. could you please show me an example on how to store in txt or dat and a sample of full code for that matter, i know i'm asking a lot thanks in advance – GuestofHonor Dec 16 '13 at 23:42
  • @GuestofHonor I've posted a fairly detailed guide on how to do what I suggested as an answer – Kevin Pei Dec 16 '13 at 23:58

1 Answers1

1

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

Community
  • 1
  • 1
Kevin Pei
  • 5,800
  • 7
  • 38
  • 55
  • 1
    thanks a lot, i will give it a try tomorrow since it's already 4AM this part of the world :) thanks again – GuestofHonor Dec 17 '13 at 00:15
  • Hello, sorry for the late reply as i've been really busy. i was going to test your code now and then i noticed that the code doesnt register reposted links by same `$name` ? it just check for reposted links in general – GuestofHonor Jan 11 '14 at 10:27
  • hello, i managed to get it to work, but is there a way to make it count reposts within 5 minutes ?! and not all posts ? thanks in advance – GuestofHonor Jan 11 '14 at 13:24
  • one more thing plz, it does detect links and chats now, but it detects all links and not same links reposted :) please help me with that, thanks – GuestofHonor Jan 11 '14 at 14:51