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
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
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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