droidus Posted July 21, 2011 Share Posted July 21, 2011 how do i echo a cookie that is in the following format?: setcookie("myCloud[loginID]", $value); i tried echo $_COOKIE['myCloud[loginID]']; , but it didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/242558-cookie-help/ Share on other sites More sharing options...
premiso Posted July 21, 2011 Share Posted July 21, 2011 You don't put it into a stupid format to begin with? Although, that "should" work, but there can be multiple problems. A: Cookies do not show up till a page reload (if I remember correctly). B: You should set an expire time and path. setcookie("myCloud[loginID]", $value, time()+3600*4, "/"); Shoudl set it to expire in 4 hours valid on the root of the webroot. See if that does you any better. Quote Link to comment https://forums.phpfreaks.com/topic/242558-cookie-help/#findComment-1245717 Share on other sites More sharing options...
droidus Posted July 21, 2011 Author Share Posted July 21, 2011 why do they need a path? i tried this, but it didn't seem to work: if(isset($_COOKIE['myCloud[loginID]'])) { echo $_COOKIE['myCloud[loginID]']; } Quote Link to comment https://forums.phpfreaks.com/topic/242558-cookie-help/#findComment-1245720 Share on other sites More sharing options...
premiso Posted July 21, 2011 Share Posted July 21, 2011 why do they need a path? They do not always need it, but since you are having troubles you need to think outside of the box and try some of the other items that setcookie has to offer. And of course the isset check did not work, your cookie is not being set to begin with. So take a look at the setcookie man page and try some of the optional options for it, go one by one or try all of them at once and see if it kicks in. If that still does not work, change the cookie name to not have the braces ( [] ) and see if it works then. Quote Link to comment https://forums.phpfreaks.com/topic/242558-cookie-help/#findComment-1245723 Share on other sites More sharing options...
droidus Posted July 21, 2011 Author Share Posted July 21, 2011 it works without the braces. i guess i will have to try some other things though... Quote Link to comment https://forums.phpfreaks.com/topic/242558-cookie-help/#findComment-1245725 Share on other sites More sharing options...
cs.punk Posted July 21, 2011 Share Posted July 21, 2011 You can only use letters, numbers and underscores as a variable name... I'm guessing it would be the same for an array key.. So to sum it up don't use [ [ ] ] in your cookie's name.. Quote Link to comment https://forums.phpfreaks.com/topic/242558-cookie-help/#findComment-1245727 Share on other sites More sharing options...
droidus Posted July 21, 2011 Author Share Posted July 21, 2011 do you usually set the httponly to 1, or 0? they have an example using ['s: <?php // set the cookies setcookie("cookie[three]", "cookiethree"); setcookie("cookie[two]", "cookietwo"); setcookie("cookie[one]", "cookieone"); // after the page reloads, print them out if (isset($_COOKIE['cookie'])) { foreach ($_COOKIE['cookie'] as $name => $value) { $name = htmlspecialchars($name); $value = htmlspecialchars($value); echo "$name : $value <br />\n"; } } ?> http://php.net/manual/en/function.setcookie.php Quote Link to comment https://forums.phpfreaks.com/topic/242558-cookie-help/#findComment-1245728 Share on other sites More sharing options...
droidus Posted July 21, 2011 Author Share Posted July 21, 2011 A: Cookies do not show up till a page reload (if I remember correctly). should i be using a session instead? that's what i was using before, but i wanted to use cookies for the "remember me" feature. (or couldn't i even use sessions to also create a remember me? i think that it is usually cookies that they choose to use though...) Quote Link to comment https://forums.phpfreaks.com/topic/242558-cookie-help/#findComment-1245730 Share on other sites More sharing options...
premiso Posted July 21, 2011 Share Posted July 21, 2011 droidus, session would work a bit better in your case. If you want the "remember" me type functionality, you can just set the session to use cookies and you can set their timeout as well. You should be able to do this by editing the php.ini see session for more information. Quote Link to comment https://forums.phpfreaks.com/topic/242558-cookie-help/#findComment-1245901 Share on other sites More sharing options...
droidus Posted July 22, 2011 Author Share Posted July 22, 2011 so in essence what your saying is, use sessions for remember me? why will some use cookies? do they have pros for this certain feature? why do they have cookies then, if you can just use sessions? Quote Link to comment https://forums.phpfreaks.com/topic/242558-cookie-help/#findComment-1246157 Share on other sites More sharing options...
premiso Posted July 22, 2011 Share Posted July 22, 2011 The session requires the use of a cookie. Why people use cookies over sessions? Well leaving the same session hash for a user over time dilutes it, meaning a greater chance for someone to hi-jack their session. You still have to store something in the database to authenticate the user from, so most people opt to store a userid and a hash (a random hash stored in a database) to authenticate people from and they often re-generate this hash each time a user logs in. This way their hash is not diluted, they can be re-authenticated easily and is a bit more secure then leaving the same hash on their system for days on end. That and it is a bit easier to control an actual cookie over control a session cookie, as the session cookie requires editing the php.ini or using ini_set methods to modify and is generally done globally. Where as with cookies you can easily tailor it to user input with a simple change in the expires time. So yea. It all depends on the need. Quote Link to comment https://forums.phpfreaks.com/topic/242558-cookie-help/#findComment-1246162 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.