I’m having a problem with this really short piece of code which I cannot get to work.
Basically, I want to display two different messages, depending on whether the user is new or returning.
<?php
if(isset($_COOKIE['visit']) && ($_COOKIE['visit'] == "true"))
{
echo 'cookie set, welcome back';
}else{
echo 'cookie not set, welcome new user';
}
setcookie("visit", "true", time()+60*60*24*600);
?>
The problem is that the cookie is not being set. I don’t know what is wrong, can anyone help me?
You need to move the setcookie into the condition:
If its not working then you should check the cookie is being sent back to the script. eg your browser or some browser addon interfering.
Be aware there is a new law that if your server is based in the EU, it requires you to obtain informed consent from your visitors before you can store or retrieve any information on there computer. http://www.cookielaw.org/