jcpd910 Posted June 1, 2012 Share Posted June 1, 2012 Here is the problem, people can sign up and log in, but the second they leave the: Welcome (whatever username)! page, they get logged out. Here is the login script. <?php session_start(); // Check if he wants to login: if (!empty($_POST[username])) { require_once("connect.php"); // Check if he has the right info. $query = mysql_query("SELECT * FROM members WHERE username = '$_POST[username]' AND password = '$_POST[password]'") or die ("Error - Couldn't login user."); $row = mysql_fetch_array($query) or die ("Error - Couldn't login user."); if (!empty($row[username])) // he got it. { $_SESSION[username] = $row[username]; echo "Welcome $_POST[username]! You've been successfully logged in. <a href=\"/index.php\" title=\"Click here to go back to the homepage.\">Home Page</a></li></ul>";; exit(); } else // bad info. { echo "Error - Couldn't login user.<br /><br /> Please try again."; exit(); } } ?> <html> <head> <title>Login</title> </head> <body> <form action="login.php" method="post"> <table width="75%" border="1" align="center" cellpadding="3" cellspacing="1"> <tr> <td width="100%"><h5>Login</h5></td> </tr> <tr> <td width="100%"><label>Username: <input type="text" name="username" size="25" value="<? echo $_POST[username]; ?>"></label></td> </tr> <tr> <td width="100%"><label>Password: <input type="password" name="password" size="25" value=""></label></td> </tr> <tr> <td width="100%"><input type="submit" value="Login!"></td> </tr> <tr> <td width="100%">Don't have an account? Click <a href="/register.php">Here</a> To register.</td></tr> </table> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/ Share on other sites More sharing options...
floridaflatlander Posted June 1, 2012 Share Posted June 1, 2012 Yeah, when they close their browser the session ends $_SESSION[username] = $row[username]; doesn't set a time to stay active. Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350388 Share on other sites More sharing options...
QuickOldCar Posted June 1, 2012 Share Posted June 1, 2012 Do you have a session_start(); at the beginning of your other pages? Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350389 Share on other sites More sharing options...
jcpd910 Posted June 1, 2012 Author Share Posted June 1, 2012 Do you have a session_start(); at the beginning of your other pages? No. I tried putting them in, but I don't have the exact code. Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350390 Share on other sites More sharing options...
jcpd910 Posted June 1, 2012 Author Share Posted June 1, 2012 Do you have a session_start(); at the beginning of your other pages? I'm not sure what you mean. I'm new to scripts like this. Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350391 Share on other sites More sharing options...
RobertP Posted June 1, 2012 Share Posted June 1, 2012 Do you have a session_start(); at the beginning of your other pages? I'm not sure what you mean. I'm new to scripts like this. add this to the top of all your pages <?php session_start(); .... Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350394 Share on other sites More sharing options...
jcpd910 Posted June 1, 2012 Author Share Posted June 1, 2012 That will still require them to log in right? I don't want guests. Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350397 Share on other sites More sharing options...
RobertP Posted June 1, 2012 Share Posted June 1, 2012 as long as you set the $_SESSION['myValue'] and don't unset it until the users want to logout, then correct; no guests. Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350400 Share on other sites More sharing options...
jcpd910 Posted June 1, 2012 Author Share Posted June 1, 2012 Can you give me the coding for that? I don't know what to put in myValue. Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350402 Share on other sites More sharing options...
RobertP Posted June 1, 2012 Share Posted June 1, 2012 $_SESSION[username] = $row[username]; the above is take from your code offtopic: taken from your code above. if you want users to logout.. session_destory(); Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350407 Share on other sites More sharing options...
jcpd910 Posted June 1, 2012 Author Share Posted June 1, 2012 Ugh! This is so hard to understand. I am using <?php session_start(); ?> At the top of my index page but I don't understand what you mean why setting and unsetting. Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350409 Share on other sites More sharing options...
RobertP Posted June 1, 2012 Share Posted June 1, 2012 just simply add session_start(); to the top of all your pages.. Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350411 Share on other sites More sharing options...
jcpd910 Posted June 1, 2012 Author Share Posted June 1, 2012 Don't use <?php> than? just copy and paste: session_start(); ? Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350412 Share on other sites More sharing options...
jcpd910 Posted June 1, 2012 Author Share Posted June 1, 2012 I just realized I don't really have any way of know if this code is working, can you give me a script that says something like: Welcome (username)! so I know whether or not you can stay logged in? I've tried other codes but they don't work with my scripts. Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350414 Share on other sites More sharing options...
floridaflatlander Posted June 1, 2012 Share Posted June 1, 2012 if (!empty($row[username])) // he got it. { $_SESSION[username] = $row[username]; echo "Welcome $_POST[username]! You've been successfully logged in. <a href=\"/index.php\" title=\"Click here to go back to the homepage.\">Home Page</a></li></ul>";; exit(); } I don't know why you have two semi colons above. you need session_start(); at the top of every page that you're going to use sessions, it needs to be the first thing on the page after the opening php tag and a few comments if any. you can echo "Welcome {$row['username']}! You've been successfully logged in. <a href=\"/index.php\" title=\"Click here to go back to the homepage.\">Home Page</a></li></ul>"; or echo "Welcome {$_SESSION[username]} You've been successfully logged in. <a href=\"/index.php\" title=\"Click here to go back to the homepage.\">Home Page</a></li></ul>"; or get rid of all that double quote stuff echo 'Welcome '.$_SESSION[username].'! You've been successfully logged in. <a href="/index.php" title="Click here to go back to the homepage.">Home Page</a></li></ul>'; Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350432 Share on other sites More sharing options...
jcpd910 Posted June 2, 2012 Author Share Posted June 2, 2012 The topic was solved by QuickOldCar who I messaged privately. Quote Link to comment https://forums.phpfreaks.com/topic/263504-users-cannot-stay-logged-in-to-my-website/#findComment-1350445 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.