How to make a cookie last only one request after being created in php

Asked

Viewed 64 times

1

Good afternoon guys, my doubt is how I can make a cookie to last only one request in php

  • You can delete the coockie so use it on the first request, but what would be the purpose?

  • You clearly don’t know what a request means, if you did, it wouldn’t make sense to use Sesssions, let alone cookies. Try to explain to us what you intend to get as a final result, what you want to do and we show you the way.

  • I’m only using it to send an error or success message, I use the setcookie() and do a header() to where I was, and ask in one place to show the cookie msg, but wanted q it disappears when I made another request

  • I think the idea of putting the error message in a really weird cookie... I understand the logic - using the header to redirect - but it still sounds like gambiarra. I advise to try not to do so, because the chance to give problem later is great.

1 answer

4


On the error display page, you simply display the cookie and delete it immediately after:

<?php
  if (isset($_COOKIE["message"]))
  {
    echo $_COOKIE["message"];
    unset($_COOKIE["message"]);
    // ^-- Aqui você exclui o cookie após a primeira requisição.
  }
?>

Remember to set an expiration date of the cookie sufficient for the next request to be made, but not so large that, if the window is closed before the second request is completed, display the error message when the user returns the page.

Browser other questions tagged

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