Run code only once

Asked

Viewed 178 times

0

Think about this, I’m creating a code "363636". With this code, I’m giving the client the option to type in the input. However, I want this code to be executed only once. That is, when it enters the code in the input, it is over. If it passes this code to someone else using this script it is no longer accepted. The code I did was the following.

I created a variable called $prenchimento, with the coupon code.

$preenchimento = "363636";  // CUPOM

So I created another variable called coupon, where you get the fill variable

$cupom = $preenchimento // NÃO MEXER

In the form, I am picking up the information via $_POST. That is, when the customer enters the coupon he does the check via if. Always checking if the digital value corresponds with the variable $coupon :

    if($tipodeinscricao == $cupom) { 

     echo "Cupom Validade";
}else{

     echo "Cupom não validado";
}

The viable $type registration is the one being picked up from the customer via $_POST. As I said, I need the customer to use this code only once, how can I do that ?

  • You need to store this value somewhere to check if it has already been typed. It can be a database, a cookie, a Sesssion, for example... the latter two do not guarantee security... for example, the cookie can be deleted, the Sesssion can be expired when closing the site...

  • There is no other way ?

  • If you want to save the value permanently, only with a database. The tb cookie works, but if the guy understands this, he can delete it easily.

  • How could I do with the cookie ?

  • Ah, you can create a file on the server and save information on it.

  • To use cookies, use the function setcookies, but in your case the ideal is database, example Sqlite.

  • The cookie would not work, because if you use the code in another browser would be validated.

Show 2 more comments

1 answer

1


Since using database (which would be more recommended) is not an option, and using cookie would not be good because the cookie is only valid in the browser that created it, and can be deleted, you can use a text file to save the information to the server and do the check.

The file will store the typed code, and you can check if that code exists inside the file.

Create a text file, for example codigos_usados.txt (only one example of a name) on your server. Then enter the code sent by the client (each code sent will be inserted into a new line within the file). The whole code would look like this:

<?php
// essa parte do código no $_POST você disse que já tem
$tipodeinscricao = $_POST['nome_do_campo'];

$preenchimento = "363636";  // CUPOM
$cupom = $preenchimento; // NÃO MEXER

$nome_arquivo = 'codigos_usados.txt'; // nome do arquivo de texto

$valido = false; // crio um flag

if($tipodeinscricao == $cupom) { 

    // aqui eu verifico se o código existe no arquivo
    $arq = file_get_contents($nome_arquivo);

    // o IF abaixo retorna false se o código não existir no arquivo
    if( !preg_match('/'.$tipodeinscricao.'/',$arq) ) {

        // aqui eu gravo o código no arquivo
        $fp = fopen($nome_arquivo,"a");
        $codigo = $tipodeinscricao.PHP_EOL;
        fwrite($fp, $codigo);
        fclose($fp);

        $valido = true; // valido a flag

    }
}

if($valido){
    echo "Cupom Validado";
}else{
    echo "Cupom não validado ou já usado";
}
?>
  • Hello!!!! , Sorry for the delay, I will check and reply to you !

  • Hello! , Thank you very much, it worked perfectly master !

  • @Eusounoob Legal!

  • I have just one question. For example, if I want to use more than one coupon, could I use an array? $fill = array("363636","2020", "5050") or this might not work ?

  • @Eusounoob Has a way. Just vc pull the code you want inside the array. But be careful, the codes need to have a pattern. For example, if you have "363636" and you put another code "6363" you will find both.

  • Got it. Thanks a lot for your help

Show 1 more comment

Browser other questions tagged

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