jaku78 Posted April 2, 2007 Share Posted April 2, 2007 Hey, I'm trying to get cookies to work on other pages of my website so when a user logs in, the user gets other functions on other webpages... So I have two codes to do this. One that lets the user log in, and if the information is right, a cookies are set. <?php mysql_connect ("localblah", "localblah", "localblah) or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("localblah"); $userentry=$_POST['username']; $passentry=$_POST['password']; $hashedpwentry=md5($passentry); $sql = "SELECT * FROM users WHERE username='$userentry'"; $result = mysql_query($sql); $userinfo = mysql_fetch_assoc($result) or die("Your username does not exist!"); if($hashedpwentry != $userinfo['password']) { die("Your login was unsuccessful, please make sure your password is correct."); } else { setcookie("username", $userinfo['username']); setcookie("password", $userinfo['password']); setcookie("userid", $userinfo['userid']); setcookie("equip", $userinfo['equip']); setcookie("name", $userinfo['name']); } ?> <html> <head> <title>Login Processed!</title> </head> <body> <a href='home.php' target='mainFrame'>Please click here to be redirected.</a> </body> </html> I'm pretty sure the error is some way I'm setting up, but I don't know what. Is there something I'm forgetting, and I need a function that will move the cookies to other variables, but I don't know what. And if there is a better way to do what I'm doing, just give me some feedback. Help is appreciated. And added to make sure if needed, here is the if code for getting the cookies I use. if (isset($_COOKIE['userid'])) Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/45340-solved-cookie-help/ Share on other sites More sharing options...
Psycho Posted April 2, 2007 Share Posted April 2, 2007 You need to set an expiration for the cookie. Otherwise the cookie expires when the session ends. Since you are not initiating a session, then it may be expiring as soon as the page changes - I've never NOT used an expiration, so I'm not 100% sure. try this: <?php $daysToLive = 30; //Days until the cookie expires setcookie("TestCookie", $value, time()+86400*$daysToLive); setcookie("username", $userinfo['username'], time()+86400*$daysToLive); setcookie("password", $userinfo['password'], time()+86400*$daysToLive); setcookie("userid", $userinfo['userid'], time()+86400*$daysToLive); setcookie("equip", $userinfo['equip'], time()+86400*$daysToLive); setcookie("name", $userinfo['name'], time()+86400*$daysToLive); Quote Link to comment https://forums.phpfreaks.com/topic/45340-solved-cookie-help/#findComment-220146 Share on other sites More sharing options...
per1os Posted April 2, 2007 Share Posted April 2, 2007 Also if you post ed that code straight you should have a syntax error, your quotes are messed up in the mysql_connect call. Quote Link to comment https://forums.phpfreaks.com/topic/45340-solved-cookie-help/#findComment-220149 Share on other sites More sharing options...
jaku78 Posted April 2, 2007 Author Share Posted April 2, 2007 Straight besides the fact I editted the mysql_connect for personal reasons, and I forgot a " at the end, but don't worry, it isn't a problem in the script I made. You guys don't mind me asking either, but when I want to log out, how do I make it work with the IF statement correctly. This statement: if (isset($_COOKIE['userid'])) If I use unset to unset the cookie, I'm pretty sure that would not unset the WHOLE global $_cookie array. Is there a certain function to delete the cookie so that people can log out? Or is there a better condition. Quote Link to comment https://forums.phpfreaks.com/topic/45340-solved-cookie-help/#findComment-220160 Share on other sites More sharing options...
per1os Posted April 2, 2007 Share Posted April 2, 2007 you have to use setcookie to kill a cookie. IE: if (isset($_COOKIE['userid'])) { setcookie('userid', '', time()-3600); } that will kill the cookie. Quote Link to comment https://forums.phpfreaks.com/topic/45340-solved-cookie-help/#findComment-220163 Share on other sites More sharing options...
Psycho Posted April 3, 2007 Share Posted April 3, 2007 that will kill the cookie. Poor cookie. Quote Link to comment https://forums.phpfreaks.com/topic/45340-solved-cookie-help/#findComment-220328 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.