Hi
I’m not sure if cookies are the best thing to be using for what I’m trying to achieve but hoping you can give me some advice.
I need a page to check if a cookie exists and if it does to show “No”.
If the cookie doesn’t exist I need the page to firstly create a cookie and then display “Yes”.
Is that possible and if so how would I code that?
I have the following code to set a cookie which will last for 1 month:
$expire=time()+60*60*24*30;
setcookie("status", "yes", $expire);
I have the following code which checks if a cookie exists and if it does shows some details:
if (isset($_COOKIE["status"]))
echo "Been before? " . $_COOKIE["status"] . "!<br />";
else
echo "Welcome guest!<br />";
Thanks for any help in advance with this.
if (isset($_COOKIE["status"])) {
echo "No";
else {
$expire=time()+60*60*24*30;
setcookie("status", "yes", $expire);
echo "Yes";
}
Firstly… sorry about the errors in the code… my bad.
You said that you want to show ‘no’ if the cookie exists…?
Remember that your cookie is valid for 1 month… so if it is set, your page will show ‘no’ for a month.
If you want to test this, you should clear the cookies before loading the page and it should show ‘yes’.
Alternatively, try setting the cookie expiration time to 10 seconds… if you refresh the page in under 10 seconds it should say no, and if you wait longer than 10 seconds it should say yes and set the cookie again.
Much like [fphp]header[/fphp] and [fphp]session_start[/fphp], setcookie must be called before any other output. 
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.
Is this the case here?
Thanks for the quick reply!
I’m obviously being a bit thick about this.
I’ve used your code as follows, I put in a missing } as it was throwing up errors but I can’t get it to work.
if (isset($_COOKIE["status"])) {
echo "No";}
else {
$expire=time()+60*60*24*30;
setcookie("status", "yes", $expire);
echo "Yes";
}
I load the page the page show “No” I reload the page and it still shows “No”. When I reload the page should it not now show “Yes” if it has set the cookie?
Told you I was being a bit thick with this!! Thanks very much for the help, really appreciate it, works a dream.