Cannot View Cookies

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!

Moderator: General Moderators

Post Reply
wallstreet
Forum Newbie
Posts: 6
Joined: Tue May 29, 2007 8:07 am

Cannot View Cookies

Post 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]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Cannot View Cookies

Post 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).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The data from the cookie won't be available until the next request.
wallstreet
Forum Newbie
Posts: 6
Joined: Tue May 29, 2007 8:07 am

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
wallstreet
Forum Newbie
Posts: 6
Joined: Tue May 29, 2007 8:07 am

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

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