Run code only once

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 he type the code in the input, it's over !. If he passes this code to someone else using this script is no longer accepted. The code I made was as follows.

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

$preenchimento = "363636";  // CUPOM

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

$cupom = $preenchimento // NÃO MEXER

In the form, I'm getting the information via $ _POST. That is, when the customer types the coupon it checks via. Always checking if the digital value matches the $ coupon variable:

    if($tipodeinscricao == $cupom) { 

     echo "Cupom Validade";
}else{

     echo "Cupom não validado";
}

The filetype $ directorytype is what is being taken from the client via $ _POST. As I've already said, I need the client to use this code only once, how can I do this?

    
asked by anonymous 30.12.2017 / 19:49

1 answer

0

Since using a 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 the created, besides being able to be deleted, you can use a text file to save the information on the server and to do the verification.

The file will save the typed code, and you can check if this code exists inside the file.

Create a text file, for example codigos_usados.txt (just an example of a name) on your server. Then enter the code sent by the client (each sent code will be inserted into a new line inside 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";
}
?>
    
30.12.2017 / 21:41