xgd Posted July 12, 2009 Share Posted July 12, 2009 Hello, I have made this registration script with all th features i wanted except "remembre me" feature, which i want to add to the login page but dont know how. I am using sessions, i know about the time() parameter for cookies but cannot squezze it all in to get what i want, so can someone help me out ? So, just a checkbox that if checked keeps the user logged in forever. this is my login script: <?php session_start(); if (isset($_POST['submit'])) { require_once('db_connect.php'); if (empty($_POST['email'])) { echo "didnt enter EMAKL<br />"; } else { $e = trim($_POST['email']); } if (empty($_POST['pass'])) { echo "Didnt enter PASS<br />"; } else { $p = trim($_POST['pass']); } if ($e && $p) { $q = "SELECT user_id, first_name, user_level FROM users WHERE email='$e' AND pass=SHA1('$p')"; $r = mysqli_query($dbc, $q) or trigger_error("Couldnt execute query 1"); if (mysqli_num_rows($r) == 1) { $_SESSION = mysqli_fetch_array($r); header("Location: index.php"); exit(); } else { echo "Youre not in hte DB<br />"; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> <!-- .style2 {color: #333333} .style3 {color: #ECE9D8} --> </style> </head> <body> <p><a href="register.php">Register</a> <a href="login.php">Login</a> <a href="password.php">Change Password</a> <a href="view_users.php">View Users</a> <a href="private.php">private</a></p> <h1>Login</h1> <form action="login.php" method="post"> <input type="text" name="email" /> <input type="text" name="pass" /> <input type="submit" name="submit" value="Login" /> </form> </body> </html> Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/ Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 http://be2.php.net/manual/en/function.session-set-cookie-params.php Default is 0 which means until the browser is closed. Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-873959 Share on other sites More sharing options...
xgd Posted July 12, 2009 Author Share Posted July 12, 2009 i still dont know where exactly or how to squezze in the (if checkbox is checked then...) and still dont understand how to make it last forever. Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-873968 Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 <input type="checkbox" name="session_lifetime" value="remember_me"> if (!empty($_POST['session_lifetime'])) { session_set_cookie_params(86400);//24 hours } session_start(); Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-873970 Share on other sites More sharing options...
xgd Posted July 12, 2009 Author Share Posted July 12, 2009 <?php session_start(); if (isset($_POST['submit'])) { require_once('db_connect.php'); if (empty($_POST['email'])) { echo "didnt enter EMAKL<br />"; } else { $e = trim($_POST['email']); } if (empty($_POST['pass'])) { echo "Didnt enter PASS<br />"; } else { $p = trim($_POST['pass']); } if ($e && $p) { $q = "SELECT user_id, first_name, user_level FROM users WHERE email='$e' AND pass=SHA1('$p')"; $r = mysqli_query($dbc, $q) or trigger_error("Couldnt execute query 1"); if (mysqli_num_rows($r) == 1) { $_SESSION = mysqli_fetch_array($r); header("Location: index.php"); exit(); } //i HERE IS THE CHANGE if(isset($_POST['remember'])){ setcookie("cookid", $_SESSION['user_id'], time()+60*60*24*100); setcookie("cookname", $_SESSION['first_name'], time()+60*60*24*100); setcookie("cooklevel", $_SESSION['user_level'], time()+60*60*24*100); } //THE CHANGE ENDS HERE else { echo "Youre not in hte DB<br />"; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> <!-- .style2 {color: #333333} .style3 {color: #ECE9D8} --> </style> </head> <body> <p><a href="register.php">Register</a> <a href="login.php">Login</a> <a href="password.php">Change Password</a> <a href="view_users.php">View Users</a> <a href="private.php">private</a></p> <h1>Login</h1> <form action="login.php" method="post"> <input type="text" name="email" /> <input type="text" name="pass" /> <input type="submit" name="submit" value="Login" /> <label> <input type="checkbox" name="remember" id="remember" /> Remember me:</label> </form> </body> </html> i put what you told me in the same place but it didnt work out. Can you modify the script so that it contains the checkbox and the codethat always has the user logged in if checked, and post the WHOLE script ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-873982 Share on other sites More sharing options...
xgd Posted July 12, 2009 Author Share Posted July 12, 2009 BTW if i can add the same functionality ussing sessions only (without cookies), thats fine too. Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-873993 Share on other sites More sharing options...
xgd Posted July 12, 2009 Author Share Posted July 12, 2009 I also tried to modify that part by puttring the cookies before such as: $q = "SELECT user_id, first_name, user_level FROM users WHERE email='$e' AND pass=SHA1('$p')"; $r = mysqli_query($dbc, $q) or trigger_error("Couldnt execute query 1"); if (mysqli_num_rows($r) == 1) { $_SESSION = mysqli_fetch_array($r); if(isset($_POST['remember'])){ setcookie("cookid", $_SESSION['user_id'], time()+60*60*24*100); setcookie("cookname", $_SESSION['first_name'], time()+60*60*24*100); setcookie("cooklevel", $_SESSION['user_level'], time()+60*60*24*100); header("Location: index.php"); exit(); } } //IT ENDS else { but this didnt work out either Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-873998 Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 <?php if (isset($_POST['submit'])) { require_once('db_connect.php'); if (empty($_POST['email'])) { echo "didnt enter EMAKL<br />"; } else { $e = trim($_POST['email']); } if (empty($_POST['pass'])) { echo "Didnt enter PASS<br />"; } else { $p = trim($_POST['pass']); } if ($e && $p) { $q = "SELECT user_id, first_name, user_level FROM users WHERE email='$e' AND pass=SHA1('$p')"; $r = mysqli_query($dbc, $q) or trigger_error("Couldnt execute query 1"); if (mysqli_num_rows($r) == 1) { if(isset($_POST['remember'])) { session_set_cookie_params(86400); } session_start(); $_SESSION = mysqli_fetch_array($r); setcookie("cookid", $_SESSION['user_id'], time()+60*60*24*100); setcookie("cookname", $_SESSION['first_name'], time()+60*60*24*100); setcookie("cooklevel", $_SESSION['user_level'], time()+60*60*24*100); header("Location: index.php"); exit(); } else { echo "Youre not in hte DB<br />"; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> <!-- .style2 {color: #333333} .style3 {color: #ECE9D8} --> </style> </head> <body> <p><a href="register.php">Register</a> <a href="login.php">Login</a> <a href="password.php">Change Password</a> <a href="view_users.php">View Users</a> <a href="private.php">private</a></p> <h1>Login</h1> <form action="login.php" method="post"> <input type="text" name="email" /> <input type="text" name="pass" /> <input type="submit" name="submit" value="Login" /> <label> <input type="checkbox" name="remember" id="remember" /> Remember me:</label> </form> </body> </html> I also took the liberty to format your code to something more readable. Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874001 Share on other sites More sharing options...
xgd Posted July 12, 2009 Author Share Posted July 12, 2009 Hey man, Thanks for modifying. I still dont see it working, i tested it, i get logged in, but when i close the browser, reopen it and go to index.php, i am not logged in. At the index.php page, i use this to check if the user is logged in (and it works, but i am not sure if i should modify that also because of the remember me feature: <?php session_start(); if (isset($_SESSION['first_name'])) { echo "Hello man<br />"; echo "<a href=\"logout.php\">logout</a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874002 Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 I still dont see it working, i tested it, i get logged in, but when i close the browser, reopen it and go to index.php, i am not logged in. Read the contents of your cookie and check for how long it is set. Or ouput the time using session_get_cookie_params() and if ttl is still 0 then session_destroy() when you login: session_start(); if (sizeof($_SESSION)) { session_destroy(); } .. Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874009 Share on other sites More sharing options...
xgd Posted July 12, 2009 Author Share Posted July 12, 2009 This just confuses me further. To put it simply, are you bothered (or anyone else) to rewrite my original login.php script so that it contains an option for a user to always stay logged in and also if need be modify the way i check to see if the user is logged (because of the remember me feature) ? I believe it will not take more then a few minutes. Thanks for trying to help, but i am not that good with php to understand what youre pointing at. Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874014 Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 To put it simply, are you bothered (or anyone else) to rewrite my original login.php script 1) Posted in the wrong section. 2) Forget my suggestion, won't work. 3) Read the contents of your cookie using firefox and some plugin which reads cookie data. Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874021 Share on other sites More sharing options...
xgd Posted July 12, 2009 Author Share Posted July 12, 2009 How is this posted in the wrong section ? Should i put it in the freelance section ? I am not asking for someone to write an entire app for me, just help me modify this script i have already made, but cant successfully modify. Can someone help me with this ? Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874022 Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 3) Read the contents of your cookie using firefox and some plugin which reads cookie data. Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874033 Share on other sites More sharing options...
xgd Posted July 12, 2009 Author Share Posted July 12, 2009 This still hasnt been solved. Any PHP heroes out there ? Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874038 Share on other sites More sharing options...
mattal999 Posted July 12, 2009 Share Posted July 12, 2009 Try this: <?php session_start(); if (isset($_POST['submit'])) { require_once('db_connect.php'); if (empty($_POST['email'])) { echo "didnt enter EMAKL<br />"; } else { $e = trim($_POST['email']); } if (empty($_POST['pass'])) { echo "Didnt enter PASS<br />"; } else { $p = trim($_POST['pass']); } if ($e && $p) { $q = "SELECT user_id, first_name, user_level FROM users WHERE email='$e' AND pass=SHA1('$p')"; $r = mysqli_query($dbc, $q) or trigger_error("Couldnt execute query 1"); if (mysqli_num_rows($r) == 1) { $_SESSION = mysqli_fetch_array($r); //i HERE IS THE CHANGE if(isset($_POST['remember'])){ setcookie("cookid", $_SESSION['user_id'], time()+60*60*24*100); setcookie("cookname", $_SESSION['first_name'], time()+60*60*24*100); setcookie("cooklevel", $_SESSION['user_level'], time()+60*60*24*100); } header("Location: index.php"); exit(); } //THE CHANGE ENDS HERE else { echo "Youre not in hte DB<br />"; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> <!-- .style2 {color: #333333} .style3 {color: #ECE9D8} --> </style> </head> <body> <p><a href="register.php">Register</a> <a href="login.php">Login</a> <a href="password.php">Change Password</a> <a href="view_users.php">View Users</a> <a href="private.php">private</a></p> <h1>Login</h1> <form action="login.php" method="post"> <input type="text" name="email" /> <input type="text" name="pass" /> <input type="submit" name="submit" value="Login" /> <label> <input type="checkbox" name="remember" id="remember" /> Remember me:</label> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874050 Share on other sites More sharing options...
MatthewJ Posted July 12, 2009 Share Posted July 12, 2009 One of the worst things you can do is implement code you don't understand... if you do, it will only cause you more problems down the road. Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874055 Share on other sites More sharing options...
xgd Posted July 12, 2009 Author Share Posted July 12, 2009 I do try to understand the ode of course, and i will understand it if i ever get it working, i just cant make it work all the way. Mattal, what exactly did you change in the code ? also will someone reply to my question to which i believe also is a very big issue, do i need to change the way i check if the user is logged in if i want to use the remember me feature ? Currently i check it like this (and it works, but without the remember me feature): if (!isset($_SESSION('first_name'])) { header("Location: blabla.php"); exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874148 Share on other sites More sharing options...
xgd Posted July 13, 2009 Author Share Posted July 13, 2009 do i need to change the way i check if the user is logged in if i want to use the remember me feature ? Currently i check it like this (and it works, but without the remember me feature): if (!isset($_SESSION('first_name'])) { header("Location: blabla.php"); exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874404 Share on other sites More sharing options...
ignace Posted July 13, 2009 Share Posted July 13, 2009 do i need to change the way i check if the user is logged in if i want to use the remember me feature ? Currently i check it like this (and it works, but without the remember me feature): if (!isset($_SESSION('first_name'])) { header("Location: blabla.php"); exit(); } Yeah that will still work. Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874448 Share on other sites More sharing options...
xgd Posted July 13, 2009 Author Share Posted July 13, 2009 Actually it wont. I just dont understand why you keep replying if you dont really know php. I have finally made what i needed so it is all good. Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874619 Share on other sites More sharing options...
pkedpker Posted July 13, 2009 Share Posted July 13, 2009 I never used any remember me checkbox's once I login.. I close open browser i'm always logged in.. I have never touched cookies as they are completely useless.. my secret is sessions Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874692 Share on other sites More sharing options...
mattal999 Posted July 13, 2009 Share Posted July 13, 2009 I do try to understand the ode of course, and i will understand it if i ever get it working, i just cant make it work all the way. Mattal, what exactly did you change in the code ? also will someone reply to my question to which i believe also is a very big issue, do i need to change the way i check if the user is logged in if i want to use the remember me feature ? Currently i check it like this (and it works, but without the remember me feature): if (!isset($_SESSION('first_name'])) { header("Location: blabla.php"); exit(); } I moved the section of the code which checks for the login and sets the cookies, because you had it in the wrong place (you had it after the } else {, not before it). Did it work though? Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-874717 Share on other sites More sharing options...
xgd Posted July 15, 2009 Author Share Posted July 15, 2009 Mattal999. i am not sure if what you posted worked. I did it differently later and now the remember me feature works, but i also had to change the way i check if a user logged in. Quote Link to comment https://forums.phpfreaks.com/topic/165682-how-to-add-remember-me-feature/#findComment-875690 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.