pugboy Posted May 13, 2008 Share Posted May 13, 2008 Currently, I am making a citation generator for school. I am also adding a feature to save your citations into a "document". I store a 30 day cookies with a random string, but if I want to access the string right after the cookie is created, it doesn't show up. How would I get the cookie to show up on the page, or how do I communicate a variable ($docid) from one set of <?php ?> tags to another farther down the page? Thanks! Link to comment https://forums.phpfreaks.com/topic/105367-solved-issues-with-cookies/ Share on other sites More sharing options...
corbin Posted May 13, 2008 Share Posted May 13, 2008 Ahhh..... When you call setcookie(), what PHP does in the long run is it sends a header to the web client telling it to set a cookie a certain way. Then, the next time that client connects to the same webserver, if it has a cookie, it may offer it up to the webserver saying, "Hey, I have a cookie with these values." PHP parses the cookie values into a convenient $_COOKIE super global array, but it is only parses the next time around due to the nature of sending the header. So setcookie('corbin', 'isawesome'); //bad example of a cookie set but oh well. does not automatically set $_COOKIE['corbin'] to "isawesome." If all goes well, it will be on the next load that PHP will parse that cookie value. So, how do you get the "isawesome" value later in the script? Simply store what you're storing ;p. $value = "Hi, this is the cookie's value."; setcookie('name', $value); echo "The cookie was set to hold {$value} in name."; Link to comment https://forums.phpfreaks.com/topic/105367-solved-issues-with-cookies/#findComment-539659 Share on other sites More sharing options...
pugboy Posted May 13, 2008 Author Share Posted May 13, 2008 The issue is, I need to get the value in a different PHP block... Do the values carry over from one block to another? Link to comment https://forums.phpfreaks.com/topic/105367-solved-issues-with-cookies/#findComment-539663 Share on other sites More sharing options...
haku Posted May 13, 2008 Share Posted May 13, 2008 Yes. As long as they are in the same document flow (the main document + all includes), they will continue from one php block to the next. Link to comment https://forums.phpfreaks.com/topic/105367-solved-issues-with-cookies/#findComment-539679 Share on other sites More sharing options...
pugboy Posted May 14, 2008 Author Share Posted May 14, 2008 Cool! That will save me QUITE a bit of code! Link to comment https://forums.phpfreaks.com/topic/105367-solved-issues-with-cookies/#findComment-540466 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.