Jump to content

Archived

This topic is now archived and is closed to further replies.

wardo

'Simple' cookie problem

Recommended Posts

I have a page which sets a cookie when a user logs in. I'm using this code:

[code]setcookie("policyusername", $username);[/code]

The cookie is being set becuase it appear in the cookies folder in firefox. But when i try and use it in my other pages it doesnt seem to exist. I dont get any error messages or anything.

I have this bit of code on the next page which wont show the cookie.

[code]You are logged in as <?php $_COOKIE["policyusername"] ?>.[/code]

Thanks for your help.

Share this post


Link to post
Share on other sites
hello,
Not just You are logged in as <?php $_COOKIE["policyusername"] ?>. it will be You are logged in as <?php echo $_COOKIE["policyusername"] ?>.

[a href=\"http://in2.php.net/manual/en/function.setcookie.php\" target=\"_blank\"]http://in2.php.net/manual/en/function.setcookie.php[/a]

Thank you,
Karthikeyan

Share this post


Link to post
Share on other sites
Thanks for your reply but it still does not work. It just shows nothing when I put:

<?php echo $_COOKIE["policyusername"] ?>

I've also read through the manual but i cant see what the problem is.

Share this post


Link to post
Share on other sites
i think cookies are not setting in your pc. make sure that your browser allows to set cookies

Thank you,
Karthi keyan.

Share this post


Link to post
Share on other sites
Cookies are enabled on my computer. I just cant work out whats going on.

Any other ideas?

Share this post


Link to post
Share on other sites
you could do with using a few extra parameters in setcookie - one for the expiry date, and one for the path.

this will make the cookie last for 30 days and allow any script within the domain to access it:
[code]
setcookie('policyusername', $username, time()+60*60*24*30, '/');
[/code]

[b]EDIT[/b]:
otherwise, make sure that $username has been set correctly before trying to save it as a cookie, and also note that cookies are only available when the page that first sets it has been reloaded or from other pages.

Share this post


Link to post
Share on other sites
first try to check is it set on your system. try to use if(isset($_COOKIE['policyusername'])) or empty($_COOKIE['policyusername']) . try to check with junk data it is $username = "try this data"; and setcookie(policyusername, $username);
and then chck it. if it shows "try this data" in browser then there is a problem in query or empty data returend by mysql.

Try all the things i told

Thank you,
Karthikeyan.

Share this post


Link to post
Share on other sites
I've tried those things but it still wont work.

when I set the cookie on the page that checks the login, and i check to see if the cookie has been set it works fine and shows me the cookie, but when i go to any other page, the cookie is not recognised.

Why would the cookie not be available on other pages?

Share this post


Link to post
Share on other sites
[!--quoteo(post=356981:date=Mar 21 2006, 02:58 PM:name=wardo)--][div class=\'quotetop\']QUOTE(wardo @ Mar 21 2006, 02:58 PM) [snapback]356981[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I've tried those things but it still wont work.

when I set the cookie on the page that checks the login, and i check to see if the cookie has been set it works fine and shows me the cookie, but when i go to any other page, the cookie is not recognised.

Why would the cookie not be available on other pages?
[/quote]

did you try setting the cookies expiry and path like i suggested?

Share this post


Link to post
Share on other sites
I tried putting the path in and now it works :)

I dont understand what it does though?

Thanks for your help.

Share this post


Link to post
Share on other sites
[!--quoteo(post=356986:date=Mar 21 2006, 03:11 PM:name=wardo)--][div class=\'quotetop\']QUOTE(wardo @ Mar 21 2006, 03:11 PM) [snapback]356986[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I tried putting the path in and now it works :)

I dont understand what it does though?

Thanks for your help.
[/quote]

in it's simplest terms, it tells the cookie where it's available to. from the manual:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.
[/quote]

me personally, i ALWAYS set it to '/' so my whole site can use it if necessary.

Cheers

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.