April 23, 2012

Php cookie only set for 1 second?

Question by Jamie Hutber

Maybe there is a better way around having to use a cookie. But i want to set in my php something to tell the user an error but only have it come on once, if they refresh the page i do no want it there anymore.

$time = (1000);
setcookie("msgtype", $type, time() + ($time), '/');
setcookie("msg", $message, time() + ($time), '/');
print_r($_COOKIE);

So for some reason, if i output the $_cookie directly after it still won’t display this cookie.

Any ideas?

Answer by gopi1410

Instead, when showing the error clear the cookie, rather than having it set for 1 second, so that when it refreshes next time & checks for the cookie it won’t be there and so would not display any error.

Or a better option would be using sessions.

Answer by Starx

You dont need to use cookies for this, using SESSION sounds reasonable.

$_SESSION['error'] = 'soem message';
//Use it when you need and simply remove it afterwards
echo $_SESSION['error'];
unset($_SESSION['error']);

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!