Run a script once yes and once no

Asked

Viewed 101 times

0

I’m having a hard time creating a logic and/or code.

I have a Landing page that I need in a simple way, preferably without using database, run a script once yes and another no.

For example:

  • User 1 register: executaScript()
  • User 2 sign up: does not execute the script
  • User 3 register: executaScript()
  • User 4 register: does not execute the script
  • User 5 register: executaScript()
  • User 6 register: does not execute the script
  • User 7 register: executaScript()
  • User 8 sign up: does not execute the script

...

I even believe it can be done using a simple database to control with true or false, but I’ve been bumping into logic for some time. Anyone can help?

Can be done in PHP or Vanilla Javascript.

3 answers

1

Users are registered where? They have some unique and sequential identification number, just do:

if($id_do_ususario % 2 === 0){
    executaScript();
}

That way when the id for 1 it will not trigger, when it is 2 it will, when it is 3 it will not execute the command... It will only execute the command on numbers divisible by 2.

  • 1

    I thought of the same solution.

  • Users are registered in a database, but during this registration a pixel is fired to count conversion in the campaign. Then this pixel needs to be fired once yes and once no, regardless of who is registering or when.

  • So, if there is an auto-increment primary key, you can use the mysqli_insert_id(), this will be the user id. This will not work if you use Mysql Transaction because it may have gaps.

0

Hello, I don’t know much about PHP, but for what you want I think a simple solution +- like this:

$file = fopen("verifica.txt", 'r');
$ler = fgets($file, 1024);

if($ler > 0){
    fwrite($file, "1"); //Grava "1" no arquivo para saber que foi executada a função
    executaScript();
} else {
    fwrite($file, "0"); //Grava "0" no arquivo para saber que NÃO foi executada a função
}

fclose($file);
  • Thanks @Daniel for the help, I had to modify the code a little to work but its logic is correct.

0

Solution to the question:

$ler = file_get_contents("verifica.txt");

if($ler == "0"){
    file_put_contents("verifica.txt","1"); //Grava "1" no arquivo para saber que foi executada a função
    executaScript();
} else {
    file_put_contents("verifica.txt","0"); //Grava "0" no arquivo para saber que NÃO foi executada a função
}

Browser other questions tagged

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