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!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi Everyone,
I created a script in my computer and the cookies worked just fine, but now after I've uploaded it to a server for some reason php cannot read the cookies.
For Example, when I've uploaded this code:
PHP Code:
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
feyd wrote:The data from the cookie won't be available until the next request.
Thank you for your comments Altough the problem still happens.
I tried the following things:
- I divided the script into 2 pages - one sets the cookie and reads it
- I checked the cookies in my browser and it's ok
- I used print_r($_COOKIE["super_admin]); instead of echo $_COOKIE["super_admin"];
- I removed the "/" domain section in the setcookie function
UPDATE: The problem suddenly solved - but only in Firefox! It's not working in IE no matter what is the privacy level - even if it's accepts all cookies!
Does anyone has idea how can I solve this problem? It's impossible that this problem happens just for me...
<html>
<head>
<title>Cookie Testing</title>
</head>
<body>
<?php
if (isset($_COOKIE['cookie_test'])) {
echo 'The cookie named "cookie_test" is set and has a value of: <strong>' . $_COOKIE['cookie_test'] . '</strong>';
} else {
setcookie('cookie_test', 'Baking_Test_Cookies', 0);
echo 'There was no cookie set yet, but I just set it for you. <a href="' . basename(__FILE__) . '">You want to see it?</a>';
}
?>
</body>
</html>
Run that script in a new file after clearing your cache, cookies, temporary files and everything else.
Likely, that's because the time is in the past for your machine, meaning it will be deleted by the browser instead of set.
If memory serves, many browsers will parse the Date header to understand what time the server thinks it is and be able to determine the period of time between the two to create the cookie properly.
Generally, when I set a cookie, I set it decently far into the future as to avoid problems of minor time differences. This will often be a month or two into the future. I then add logic that removes the cookie when actually needed.
I agree with feyd. I had a similar problem to yours. The 'fix' for me was setting the expiry time to a year later. It had to do with the interpretation of time.