Page 1 of 1

Cannot View Cookies

Posted: Tue May 29, 2007 8:18 am
by wallstreet
Everah | Please use

Code: Select all

,

Code: Select all

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:

Code: Select all

<?
setcookie("super_password", "hello", time()+3600, "/");
echo "the cookie is ".$_COOKIE["super_password"];
?>
It's returns nothing (not returning "hello". even after I've clicked "Refresh").

Does anyone knows what's might be causing this problem?

Thanks! That's really important...

Eyal


Everah | Please use

Code: Select all

,

Code: Select all

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]

Re: Cannot View Cookies

Posted: Tue May 29, 2007 9:24 am
by superdezign
wallstreet wrote:Does anyone knows what's might be causing this problem?
No.

.... Is the cookie actually being created? Are cookies enabled on your browser?

As far as I know, you can't disable cookies within PHP (unless you mess with the whole GPCS thing).

Posted: Tue May 29, 2007 9:30 am
by feyd
The data from the cookie won't be available until the next request.

Posted: Wed May 30, 2007 2:09 am
by wallstreet
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...

Eyal

Posted: Wed May 30, 2007 10:56 am
by RobertGonzalez
Here is one of the easiest tests to check that cookies are working:

Code: Select all

<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.

Posted: Thu May 31, 2007 8:30 am
by wallstreet
I found out something weird: IE accepts my cookie only if it's time being set to 0 like that:

setcookie("name", "value", 0, "/");

I cannot put something like "time()+3600" - it's don't accepts it.
Does somebody know how can I solve that problem?[/b]

Posted: Thu May 31, 2007 8:34 am
by feyd
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.

Posted: Thu May 31, 2007 10:18 am
by RobertGonzalez
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.