Rule to validate PHP value record

Asked

Viewed 57 times

0

I have the following code:

$newprice = $ob->pennyauction == 1 ? $oldprice + 0.01 : $oldprice + $plusprice;

I need it to perform the following check, before displaying the value of $newprice.

The rule is to prevent the same value being written to DB, for example if in $oldprice you already have 0.02 and he tries to record the 0.02 again, the rule should add + 0.01.

He already does it there, but sometimes it happens to run at exactly the same time, then he inserts the same values, then enters this rule to validate it.

There’s something to implement there?

  • Will depend, run at exactly the same time .. you mean multiple users at the same time ? Or you are running a loop ?

  • Multiple users at the same time.

  • When running add in a server side execution queue instead of running on time should prevent this

  • @Anthraxisbr Do you have any examples of this? I have no idea what it is.

  • See if this resolves: https://www.exakat.io/prevent-multiple-php-scripts-at-the-same-time/ lock file is simpler than creating that list

  • Boy, I read everything on that website and I don’t understand exactly what it’s gonna do. Will prevent the file from running at the same time by users?

  • Yes, when running a script, creating a file and when finished it removes, and if you try to run at the same time it tries to create that file, if it exists it does not let run, I will make a response.

Show 2 more comments

1 answer

0

You can do this in two ways, with an execution queue, or with a file to lock the simultaneous execution, the idea is:

1 - While running, create a file . lock (can be any file extension, but we will use .lock)

2 - Run the actual script

3 - When finished remove the file

Example:

if (mkdir('/tmp/prevent.lock', 0700)) {
    funcaoDaRegra();
    rmdir('/tmp/prevent.lock');
} else {

    funcaoPreventiva();
}

function funcaoDaRegra() {
    //Faça aqui o que deve ser feito sem duplicar
    echo 'Aqui a função foi executada';
}

function funcaoPreventiva() {
    //De uma resposta ao usuário caso tenha sido prevenida a duplicação
    echo 'Aqui prevenimos a execução dupla';
}

NOTE: If you are using windows server, use mkdir with the third parameter:

mkdir('/tmp/prevent.lock', 0700,true)
  • But that wouldn’t be problematic, since my file runs virtually every second?

  • But they are every second 100% of the time ? to not have too many error answers, in the request, instead of responding that can not be executed first, try about 3 times in a quick interval, will hardly stop.

  • Then.. It is a penny auction system. The file will run several times only when an auction starts. If there is no auction running it will not run multiple times.

  • I tried to implement but it didn’t work very well.

  • How so didn’t work out very well ? makes an ideone of how you tried to apply

Browser other questions tagged

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