PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
setcookie('test',1);
if ($_COOKIE['test']==1)
{
echo "cookie is set";
}
else
{
echo "cookie is not set";
}
?>
However the very first time the page is accessed, the $_COOKIE['test'] returns nothing, but when I refresh the page it returns 1, resulting in a false "You don't have cookies enabled" message at the start. It seems php has problems getting the cookie value right after it sets it. Is there a way to fix this?
When you set a cookie, you have to reload the page to make it take effect. Use a header() redirect after setting it, to the same page, effectively refreshing it.
Yep, because of your logic. If the user enters your page with cookies disabled, it will detect there's no cookie, and redirect them / refresh the page. Then it will detect they have no cookie, and refresh the page... etc. Maybe put the cookie setting code on one page then redirect to another?