In a chat room, how can I detect and warn that a certain system user is sending lots of messages in a row?
In a chat room, how can I detect and warn that a certain system user is sending lots of messages in a row?
I suggest saving the session the date the message was sent and a message counter. If the user sends another message, compare the date with which it is stored in the session, if the time difference is short, increment a counter in the session.
If this counter reaches a limit (eg 3 messages) refuse the next requests until a certain time has passed.
To save the time of the 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 posts per second limit
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"]);
// ...
}