How can I detect and alert that a particular user is sending many messages in a row?

Asked

Viewed 244 times

5

In a chat room, how can I detect and alert that a certain system user is sending many messages in a row (Flood)?

1 answer

7


I suggest you save in the session the date the message was sent and a message counter. If the user sends another message, compare the date with the one stored in the session, if the time difference is short, increment a counter in the session.

If this counter reaches a limit (ex: 3 messages) refuse the next requests until a certain time has passed.

To save time from last post

$_SESSION["LastPost"] = time();

To compare the time of the post

// se o post foi feito novamente nos últimos 10 segundos
if (isset($_SESSION["LastPost"])
    && $_SESSION["LastPost"] <= (time() - 10))
{
    if (!isset($_SESSION["PostCount"]))
        $_SESSION["PostCount"] = 0;

    $_SESSION["PostCount"] += 1;
}

And to check if the user has reached the limit of posts per second

if (isset($_SESSION["PostCount"]) && $_SESSION["PostCount"] == 3)
{
    // você pode setar uma trava
    $_SESSION["LockPost"] = time() + 60;
}

If the lock exists ignore the request.

if (isset($_SESSION["LockPost"]) && $_SESSION["LockPost"] >= time())
{
    // bloqueado, ignore
}
else if (isset($_SESSION["LockPost"]))
{
    // o tempo de bloqueio passou
    // zere todas as variáveis da sessão
    unset($_SESSION["LockPost"]);
    // ...
}
  • Good answer, but 3 messages every 10 seconds is very little, because some people can type 3 messages in just over a second... :)

  • 3

    Also, for usability reasons, it would be interesting also make a redundant lock on client of chat so that the user knows what is happening. If he tries to cheat, then the server will ignore.

  • 1

    Good observation, avoid roundtrips is always a good.

  • In my case, when the server detects Flood, it will insert a private message in the chat table, which only the author of Flood will see, alerting the incident.

  • It would be nice to compare if the 3 messages have similar texts, for then yes detect Flood, because a user can also type fast.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.