Vigilant Psyche Posted April 24, 2008 Share Posted April 24, 2008 Can some one please explain the flow of creating a new cookie, setting its value to a php variable, say $value, and then reading that value? Thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/102784-cookies/ Share on other sites More sharing options...
DeanWhitehouse Posted April 24, 2008 Share Posted April 24, 2008 what is it for, if its for a login form, use sessions Quote Link to comment https://forums.phpfreaks.com/topic/102784-cookies/#findComment-526470 Share on other sites More sharing options...
Vigilant Psyche Posted April 24, 2008 Author Share Posted April 24, 2008 I did but they don't disappear after a set time, which is what I need to happen... Quote Link to comment https://forums.phpfreaks.com/topic/102784-cookies/#findComment-526475 Share on other sites More sharing options...
DeanWhitehouse Posted April 24, 2008 Share Posted April 24, 2008 are you setting the sessions on every page with session_start(); Quote Link to comment https://forums.phpfreaks.com/topic/102784-cookies/#findComment-526476 Share on other sites More sharing options...
Vigilant Psyche Posted April 24, 2008 Author Share Posted April 24, 2008 Yes, I have the sessions working perfectly fine but I would like to use cookies instead due to their time-limit. Quote Link to comment https://forums.phpfreaks.com/topic/102784-cookies/#findComment-526483 Share on other sites More sharing options...
GregL83 Posted April 24, 2008 Share Posted April 24, 2008 Your browser may need to refresh after the cookie is disabled. Try closing down the browser and re-opening it. I do something like this: <?php session_start(); setcookie('value', $value, time() + 3600, "/"); echo $_COOKIE['value']; ?> to delete I use: <?php session_start(); setcookie('value', '', time() - 3600, "/"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/102784-cookies/#findComment-526487 Share on other sites More sharing options...
Vigilant Psyche Posted April 24, 2008 Author Share Posted April 24, 2008 Okay thanks. Now when I change the value of the cookie after initially setting it do i have to use the function each time or just $_COOKIE['value'] because I heard that the setcookie funtion has to be before htmkl tag. Quote Link to comment https://forums.phpfreaks.com/topic/102784-cookies/#findComment-526493 Share on other sites More sharing options...
GregL83 Posted April 24, 2008 Share Posted April 24, 2008 Anytime you want to pull the variable use $_COOKIE['value']. Make sure that the session is started by session_start() ... Thats it. Quote Link to comment https://forums.phpfreaks.com/topic/102784-cookies/#findComment-526496 Share on other sites More sharing options...
Vigilant Psyche Posted April 24, 2008 Author Share Posted April 24, 2008 Okay so this? <?php session_start(); setcookie("user", "", time()+3600); ?> <HTML> <BODY> //form $_COOKIE['user']=$_POST['name']; //(from form) echo $_COOKIE['user']; </BODY> </HTML> Quote Link to comment https://forums.phpfreaks.com/topic/102784-cookies/#findComment-526497 Share on other sites More sharing options...
GregL83 Posted April 24, 2008 Share Posted April 24, 2008 sorry if I didn't explain it better.... I set the cookie in the body of the code and/or delete it there as well. <html> <head></head> <body> <?php # start session session_start(); # create cookie setcookie('value', $value, time() + 3600, "/"); # display cookie; after page has been refreshed echo $_COOKIE["value"]; # deleting cookies setcookie('value', "", time() - 3600, "/"); ?> </body> </html> if anyone notices and error feel free to let us know, I use this method and it works for me... Quote Link to comment https://forums.phpfreaks.com/topic/102784-cookies/#findComment-526505 Share on other sites More sharing options...
Vigilant Psyche Posted April 24, 2008 Author Share Posted April 24, 2008 <html> <head></head> <body> <?php # start session session_start(); # create cookie setcookie('value', $value, time() + 3600, "/"); $_COOKIE['value']=$value; <-----------------------------------------------------is this valid, or do I use the function again? # display cookie; after page has been refreshed echo $_COOKIE["value"]; # deleting cookies setcookie('value', "", time() - 3600, "/"); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/102784-cookies/#findComment-526513 Share on other sites More sharing options...
GregL83 Posted April 25, 2008 Share Posted April 25, 2008 not sure, but I think you have to call it using setcookie again.... try it out... anybody know? Quote Link to comment https://forums.phpfreaks.com/topic/102784-cookies/#findComment-526739 Share on other sites More sharing options...
DarkWater Posted April 25, 2008 Share Posted April 25, 2008 Can you set a cookie, read its value, and destroy it in one execution of a script? I didn't think so. >_> Cookie values become available on the next page, which is when you can destroy it properly too. Quote Link to comment https://forums.phpfreaks.com/topic/102784-cookies/#findComment-526743 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.