Jump to content


Photo

'Simple' cookie problem


  • Please log in to reply
10 replies to this topic

#1 wardo

wardo
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 21 March 2006 - 01:49 PM

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

setcookie("policyusername", $username);

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.

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

Thanks for your help.
www.realdesignnetwork.com

#2 blazing_bruce

blazing_bruce
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationIndia

Posted 21 March 2006 - 02:04 PM

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

#3 wardo

wardo
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 21 March 2006 - 02:13 PM

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.
www.realdesignnetwork.com

#4 blazing_bruce

blazing_bruce
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationIndia

Posted 21 March 2006 - 02:23 PM

i think cookies are not setting in your pc. make sure that your browser allows to set cookies

Thank you,
Karthi keyan.

#5 wardo

wardo
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 21 March 2006 - 02:27 PM

Cookies are enabled on my computer. I just cant work out whats going on.

Any other ideas?
www.realdesignnetwork.com

#6 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 02:35 PM

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:
setcookie('policyusername', $username, time()+60*60*24*30, '/');

EDIT:
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.
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#7 blazing_bruce

blazing_bruce
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationIndia

Posted 21 March 2006 - 02:36 PM

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.

#8 wardo

wardo
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 21 March 2006 - 02:58 PM

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?
www.realdesignnetwork.com

#9 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 03:00 PM

[!--quoteo(post=356981:date=Mar 21 2006, 02:58 PM:name=wardo)--][div class=\'quotetop\']QUOTE(wardo @ Mar 21 2006, 02:58 PM) View Post[/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?
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#10 wardo

wardo
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 21 March 2006 - 03:11 PM

I tried putting the path in and now it works :)

I dont understand what it does though?

Thanks for your help.
www.realdesignnetwork.com

#11 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 03:16 PM

[!--quoteo(post=356986:date=Mar 21 2006, 03:11 PM:name=wardo)--][div class=\'quotetop\']QUOTE(wardo @ Mar 21 2006, 03:11 PM) View Post[/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

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users