Execute a condition only once

0

I've created a script that checks if the number of posts is equal to 2, in case you get a bonus update ). But I just wanted it to check once and not every time I refresh the page.

How can I do this so that you check only once?

$selectPost = selectPos();

//select number of posts
if($selectPost == 2) {
    update_Post_Bonus($db, $steamid);
}
    
asked by anonymous 06.05.2017 / 02:23

1 answer

0
  

But I just wanted it to check once and not every time I refresh the page.

A very basic way, would you store a variable in the client session :

$selectPost = selectPos();

//select number of posts
if($selectPost == 2 && !isset($_SESSION['recebeu_bonus'])) {
    update_Post_Bonus($db, $steamid);
    $_SESSION['recebeu_bonus'] = true;
}

If you are not using a session in your project, be sure to add start_session() at the start of your script .

  

Session Usage Guide : PHP: > Session Variables

Another option if you have some login system , would be to create a table in your database that stores the User ID when receiving the bonus. Hence, just create a function that checks if the user has received the bonus and put it in the condition:

$selectPost = selectPos();

//select number of posts
if($selectPost == 2 && !recebeu_bonus($id_do_usuario)) {
    update_Post_Bonus($db, $steamid);
    registrar_bonus($id_do_usuario); // função para registrar a ID do usuário na tabela do bônus no banco de dados
}

We need to develop the functions to check if the user has received the bonus and to register the user upon receiving the bonus according to their project / database. And if there are several bonuses in your project, you can create a table for each bonus and insert a column in the table to register the bonus id next to the user id . p>     

06.05.2017 / 04:40