Rule to validate PHP value record

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 from being written to the DB, for example if you already have 0.02 and it tries to write 0.02 again, the rule should add + 0.01 .

It already does this there, but sometimes it happens to run exactly at the same time, so it inserts the same values, then enter that rule to validate it.

Is there anything to implement there?

    
asked by anonymous 02.03.2018 / 05:05

1 answer

0

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

1 - When running, create a .lock file (it can be any file extension, but let's use .lock )

2 - Run the actual script

3 - When you finish removing 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 a windows server, use mkdir with the third parameter:

mkdir('/tmp/prevent.lock', 0700,true)
    
02.03.2018 / 17:07