S
S
Scuba2018-05-18 16:06:46
PHP
Scuba, 2018-05-18 16:06:46

How to organize a counter of calls to the script?

There is a script.
This script is called upon certain user actions.
It can be called both on the server side via CURL POST and on the user side via AJAX.
How can a counter be organized so that a user can call the script no more than 30 times during one visit to the site? Is there a solution so that the user cannot influence the counter - cookie substitution and all that?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly, 2018-05-18
@rim89

look at sessions

R
Roman Terekhin, 2018-05-19
@RomaZveR

Do Rate Limit via Redis.
https://redis.io/commands/incr

S
stepar, 2018-05-25
@stepar

Here is an example for you

<?php ## Пример работы с сессиями.
  session_start();
  // Если на сайт только-только зашли, обнуляем счетчик.
  if (!isset($_SESSION['count'])) $_SESSION['count'] = 0;
  // Увеличиваем счетчик в сессии.
  $_SESSION['count'] = $_SESSION['count'] + 1;
?>
<h2>Счетчик</h2>
В текущей сессии работы с браузером Вы открыли эту страницу
<?= $_SESSION['count'] ?> раз(а).<br />
Закройте браузер, чтобы обнулить счетчик.<br />
<a href="<?= $_SERVER['SCRIPT_NAME'] ?>" target="_blank">Открыть дочернее окно браузера</a>.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question