Cookie expiration

Hi there,

I have an form which is handled by a script called mail.php, and I’ve set up a cookie to return a message when the form is submitted to inform to user that everything is ok, or if there are errors.

I’m a little confused about how to unset the cookie.

as far as I’m aware, I have to set the expiration time to somewhere in the past, so it will be automatically deleted by the browser.

So here’s how I set the cookie in the mail.php script:

setcookie('mail', 'Thank you for your interest, we shall be in touch soon.', time() + 10);

// some other junk

header('Location: ' . $goto); // where $goto is the page where the form was submitted from

Then on the page where the form was submitted:

//header etc
<?php if(isset($_COOKIE['mail'])) : ?>
  <div id="mail-response"><?php echo $_COOKIE['mail'] ?></div>
  setcookie('mail', '', time() - 3600);

But in this case the cookie will not expire for 10 seconds, where I want it to expire immediately, so the message is not there on refresh.

Any help much appreciated,

It sounds like you want a “flash message”, as such, wouldn’t a session be better suited?

yeah a flash message sounds like the ticket. To be honest, I’m not sure what is more ‘suitable’, but yes I can easily set up a session for this.