wardo Posted March 21, 2006 Share Posted March 21, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/ Share on other sites More sharing options...
blazing_bruce Posted March 21, 2006 Share Posted March 21, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19340 Share on other sites More sharing options...
wardo Posted March 21, 2006 Author Share Posted March 21, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19344 Share on other sites More sharing options...
blazing_bruce Posted March 21, 2006 Share Posted March 21, 2006 i think cookies are not setting in your pc. make sure that your browser allows to set cookiesThank you,Karthi keyan. Quote Link to comment https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19346 Share on other sites More sharing options...
wardo Posted March 21, 2006 Author Share Posted March 21, 2006 Cookies are enabled on my computer. I just cant work out whats going on.Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19348 Share on other sites More sharing options...
redbullmarky Posted March 21, 2006 Share Posted March 21, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19349 Share on other sites More sharing options...
blazing_bruce Posted March 21, 2006 Share Posted March 21, 2006 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 toldThank you,Karthikeyan. Quote Link to comment https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19350 Share on other sites More sharing options...
wardo Posted March 21, 2006 Author Share Posted March 21, 2006 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 Link to comment https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19354 Share on other sites More sharing options...
redbullmarky Posted March 21, 2006 Share Posted March 21, 2006 [!--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? Quote Link to comment https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19355 Share on other sites More sharing options...
wardo Posted March 21, 2006 Author Share Posted March 21, 2006 I tried putting the path in and now it works :)I dont understand what it does though?Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19359 Share on other sites More sharing options...
redbullmarky Posted March 21, 2006 Share Posted March 21, 2006 [!--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 Quote Link to comment https://forums.phpfreaks.com/topic/5420-simple-cookie-problem/#findComment-19360 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.