0
I know there are already questions about this mistake, but I need some help because I can’t solve it, even with the information about the error. I am making a website that sets a cookie to show an error and after it shows, it deletes, however, to delete the cookie, it has to be after displaying, only it gives this error, and if delete before, it does not show the error.
Example:
if($envio){ $mensagem = "Houve um erro no envio!"; }
else { $mensagem = "Enviado com sucesso!" }
setcookie('mensagem', $mensagem);
And when it’s time to show the error:
if(isset($_COOKIE['mensagem'])){
echo "<div class='alert'>";
echo $_COOKIE['mensagem'];
echo "</div>";
setcookie('mensagem'); }
Even trying to replace 'setcookie()' with 'unset()', I can’t get it to delete the cookie.
Note: I’m doing everything on a Landing page
I recommend reading carefully the linked post above, because what is explained there applies perfectly to your case. If you have given any content output, you can no longer redirect or set cookies (it is the same process, the use of headers). The solution is to do everything you need to with a header before showing things on the screen, and leave echo to the end of the script. It’s just a matter of rearranging your code.
– Bacco
I decided without cookie, because the question of reorganizing the way I wanted, I would not, because I would have to show the error and then delete, and as the headers can not be modified after being sent, I can not delete. But thank you.
– Marcos Vaz de Oliveira
Remember that you have META REFRESH, which also allows delay (but needs to be put in the head).
– Bacco
For 5 seconds of waiting:
<meta http-equiv="refresh" content="5; url=http://endereco/">
– Bacco
Have alternative to use a JS as well. Anyway, it is always good a discreet link (even if it is small) with the classic "click here if it is not redirected in X seconds", for person not to be "stuck" on the page if any restriction prevents refresh.
– Bacco