Jump to content

'Simple' cookie problem


wardo

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.
Link to comment
https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/
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
Link to comment
https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19340
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.
Link to comment
https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19349
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.
Link to comment
https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19350
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?
Link to comment
https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19354
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?
Link to comment
https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19355
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
Link to comment
https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19360
Share on other sites

Archived

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

×
×
  • Create New...

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.