guitarist809 Posted March 31, 2007 Share Posted March 31, 2007 Hello, I'm trying to set two cookies on my site, containing a username and password so I can retrieve them for later use This function doesn't set any cookies i give it function session_init($username, $password) { setcookie("tripdb_username", $rs['username'], time()+3600); setcookie("tripdb_password", $rs['password'], time()+3600); echo "cookies set!"; echo "<pre>"; print_r($_COOKIE); I'm using the print_r to show all cookies, all it shows is a PHPSESSID Why isn't this working? Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/ Share on other sites More sharing options...
genericnumber1 Posted March 31, 2007 Share Posted March 31, 2007 You can't use a cookie until the user refreshes the page. From the manual: "Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires...." Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218532 Share on other sites More sharing options...
guitarist809 Posted March 31, 2007 Author Share Posted March 31, 2007 You can't use a cookie until the user refreshes the page. From the manual: "Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires...." Yea I saw that. I refreshed the page w/o the setcookie function and still didn't see the cookies =\. Just the PHPSESSID... Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218534 Share on other sites More sharing options...
dsaba Posted March 31, 2007 Share Posted March 31, 2007 yeah well what is your page doing when you REFRESH it? exactly what it did the first time so you will get the same results won't you? what you need to do is make an if/else statement to set the cookie if it isn't already set, and display the cookie if it is set, etc... you get it whats going with ur page now is that it keeps setting and overwriting ur cookie *EDIT sorry i didn't read all of your post about how u removed the setcookie function try this when echoing the cookie: $var = $_COOKIE['urcookiename']; echo $var; because when you print_r $cookie, i think only works if u SET the cookie on that page use this other syntax and it will call on the cookie that is already set Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218537 Share on other sites More sharing options...
neel_basu Posted March 31, 2007 Share Posted March 31, 2007 You can't use a cookie until the user refreshes the page. JavaScript Cookies ?? But I would Suggest You Dont Use Cookies Use Sessions Its Much more Simple and also more secure. Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218538 Share on other sites More sharing options...
guitarist809 Posted March 31, 2007 Author Share Posted March 31, 2007 Basically it goes as follows: login.php => input username/password into a form. Submit form login.php => Make sure username/password is valid w/ mysql database. if it is, set two cookies (username and password) and do header('location: main.php'); main.php => check cookies ($_COOKIE['username'], $_COOKIE['password']) - make sure those are set. (They wern't so I did a print_r to see what was in $_COOKIE, but nothing was there... main.php => if no cookies are set (or cookies contain invalid information)... header('location: login.php'); Hope that helps with my prob... Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218541 Share on other sites More sharing options...
guitarist809 Posted March 31, 2007 Author Share Posted March 31, 2007 You can't use a cookie until the user refreshes the page. JavaScript Cookies ?? But I would Suggest You Dont Use Cookies Use Sessions Its Much more Simple and also more secure. I did use sessions, but they would self-terminate when I closed out of my browser. With cookies you can set a delete time. Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218542 Share on other sites More sharing options...
dsaba Posted March 31, 2007 Share Posted March 31, 2007 i edited my post, try that Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218544 Share on other sites More sharing options...
guitarist809 Posted March 31, 2007 Author Share Posted March 31, 2007 Still does nothing. I think that setcookie() isnt doing anything becides returning true... Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218545 Share on other sites More sharing options...
dsaba Posted March 31, 2007 Share Posted March 31, 2007 i had a similar problem when i was playing around with cookies a while ago only solution i found was to only set the cookie on ONE page and then call the cookie on ANOTHER page not to do this on the same page also sometimes if u refresh it TWICE then it works... play around with those things Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218546 Share on other sites More sharing options...
neel_basu Posted March 31, 2007 Share Posted March 31, 2007 You can't use a cookie until the user refreshes the page. JavaScript Cookies ?? But I would Suggest You Dont Use Cookies Use Sessions Its Much more Simple and also more secure. I did use sessions, but they would self-terminate when I closed out of my browser. With cookies you can set a delete time. /* set the cache expire to 30 minutes */ session_cache_expire(30); $cache_expire = session_cache_expire(); you can change the max lifetime for a session with the function ini_set(). <?php ini_set("session.gc_maxlifetime", "18000"); ?> Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218547 Share on other sites More sharing options...
guitarist809 Posted March 31, 2007 Author Share Posted March 31, 2007 You can't use a cookie until the user refreshes the page. JavaScript Cookies ?? But I would Suggest You Dont Use Cookies Use Sessions Its Much more Simple and also more secure. I did use sessions, but they would self-terminate when I closed out of my browser. With cookies you can set a delete time. /* set the cache expire to 30 minutes */ session_cache_expire(30); $cache_expire = session_cache_expire(); you can change the max lifetime for a session with the function ini_set(). <?php ini_set("session.gc_maxlifetime", "18000"); ?> That code did'nt do anything As soon as I exit my browser and start it up again i still have to re-authenticate Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218548 Share on other sites More sharing options...
dsaba Posted March 31, 2007 Share Posted March 31, 2007 But I would Suggest You Dont Use Cookies Use Sessions Its Much more Simple and also more secure. Security with cookies is controversial, do some research guitarist on cookie "security" as long as nothing private or no sensitive information is stored in cookies, its a perfectly great way to store information for long periods of time only problem is that sometimes users choose to not accept cookies, but that is rare, for those extra paranoid internet users out there, cookies and the internet go together like bread and butter *a note on the session max time, session will ALWAYS expire when the browser window is closed all the code the other dude gave u is define when the session will expire while the browser window is still open Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218549 Share on other sites More sharing options...
guitarist809 Posted March 31, 2007 Author Share Posted March 31, 2007 I'm just trying to do something similar to what PhpBB does (with theyre "Keep me logged in forever" option). And this site isn't public, its for my family and i know that we all accept cookies on our pc's (so that's not much of an issue) Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218550 Share on other sites More sharing options...
guitarist809 Posted March 31, 2007 Author Share Posted March 31, 2007 *bump* Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218556 Share on other sites More sharing options...
neel_basu Posted March 31, 2007 Share Posted March 31, 2007 What does *bump* means ?? Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218557 Share on other sites More sharing options...
guitarist809 Posted March 31, 2007 Author Share Posted March 31, 2007 lol, bump is a statment that means "My topic is falling in the list of other topics, i wanna make it first again" bump also like "Bump, its at the top now " Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218559 Share on other sites More sharing options...
guitarist809 Posted March 31, 2007 Author Share Posted March 31, 2007 so yea, can anybody help? Link to comment https://forums.phpfreaks.com/topic/45019-cookie-problems/#findComment-218690 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.