ryanmetzler3 Posted August 26, 2013 Share Posted August 26, 2013 (edited) I have a site with users. Basically you register and then when you log in it starts a session. It worked fine when I wrote it running it on WAMP. Now it is on a hosted server and it logs you out automatically as soon as you go to the next page after logging in. I assume the session is closing. Here is the session heading that I put on every page. any idea what is wrong? <?php session_start(); error_reporting(E_ALL^ E_NOTICE); $username = $_SESSION['username']; $userid = $_SESSION['userid']; ?> Here is my login page that starts the session <?php session_start(); error_reporting(E_ALL^ E_NOTICE); $username = $_SESSION['username']; $userid = $_SESSION['userid']; ?> <html> <head> <title>Login</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" type="text/css" href="main.css" media="screen" /> </head> <body> <div id='content'> <div style="clear: both;"></div> <?php include "header.php"; include "menu.php"; echo "<center><h2>Login</h2></center>"; if (isset($_POST['loginbtn']) or ($username && $userid)) {} else{echo "<right><p>Welcome back! Sign in here.</p></right>";} if ($username && $userid) { echo "You are already logged in as <b>$username</b>"; echo "<br/>Not " . $username . "? <a href='logout.php'>Logout</a>"; } else { $form = "<div id='forms'><form action='login.php' method='post'> <table class='forms'> <td>Username:</td> <td><input type='text' name='user'/></td> </tr> <tr> <td>Password:</td> <td><input type='password' name='password'/></td> </tr> <td></td> <td><input type='submit' name='loginbtn' value='Login'/></td> </tr> <tr> <td><a href='register.php'>Register</a></td> <td><a href='forgotpass.php'>Forgot Password?</a></td> </tr> </table> </form></div>"; } if ($_POST['loginbtn']) { $user = $_POST['user']; $password = $_POST['password']; if ($user) { if ($password) { require ("connect.php"); $password = md5(md5("R4E2M0".$password."R4E2M0")); $query = mysql_query("SELECT * FROM user WHERE username='$user'") or die (mysql_error()); $numrows = mysql_num_rows($query); if ($numrows ==1) { $row = mysql_fetch_assoc($query); $dbid= $row['id']; $dbuser= $row['username']; $dbpass = $row['password']; $dbactive = $row['active']; if ($password == $dbpass) { if ($dbactive == 1) { // set session info $_SESSION['userid'] = $dbid; $_SESSION['username'] = $dbuser; echo "<font color='green'>You have been logged in as <b>$dbuser</b>. <a href='member.php'>Click Here</a> to go to the member page.</a></font>"; } else { echo "<font color='red'>You must activate your account to login. $form</font>"; } } else { echo "<font color='red'>You did not enter the correct password. $form</font>"; } } else { echo "<font color='red'>The username you entered was not found.$form</font>"; } mysql_close(); } else { echo "<font color='red'>You must enter your password . $form</font>"; } } else { echo "<font color='red'><p>You must enter your username . $form</font>"; } } else{ echo $form; } ?> </div> <?php include 'footer.php'; ?> <div style="clear: both;"></div> <center><font size="2">Be sure to take all photos intended for this site from public property or from property you have permission to access. This site in no way encourages illegal graffiti.</font></center> </body> </html> Edited August 26, 2013 by ryanmetzler3 Quote Link to comment https://forums.phpfreaks.com/topic/281555-session-not-staying-open/ Share on other sites More sharing options...
jazzman1 Posted August 26, 2013 Share Posted August 26, 2013 How the server knows that this user is on the session? Quote Link to comment https://forums.phpfreaks.com/topic/281555-session-not-staying-open/#findComment-1446763 Share on other sites More sharing options...
ryanmetzler3 Posted August 26, 2013 Author Share Posted August 26, 2013 How the server knows that this user is on the session? I'm sorry I am extremely new to php and this is the first website I have ever made. Can you elaborate on what you mean by that? I never had to specify which user was associated with a session when using WAMP. Quote Link to comment https://forums.phpfreaks.com/topic/281555-session-not-staying-open/#findComment-1446765 Share on other sites More sharing options...
Solution mac_gyver Posted August 26, 2013 Solution Share Posted August 26, 2013 remove your existing error_reporting() statements and add the following two lines, immediately after the <?php tag, before the session_start() statements to see any php detected errors - ini_set("display_errors", "1"); error_reporting(-1); Quote Link to comment https://forums.phpfreaks.com/topic/281555-session-not-staying-open/#findComment-1446768 Share on other sites More sharing options...
ryanmetzler3 Posted August 26, 2013 Author Share Posted August 26, 2013 (edited) remove your existing error_reporting() statements and add the following two lines, immediately after the <?php tag, before the session_start() statements to see any php detected errors - ini_set("display_errors", "1"); error_reporting(-1); It gave me a whole mess of errors. Here they are Warning: session_start(): open(/var/php_sessions/sess_2b20e85a7fb0622645b0db32fe7ac672, O_RDWR) failed: No such file or directory (2) in /hermes/bosoraweb068/b2719/ipg.trackyourgraffiticom/includelog.php on line 4 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /hermes/bosoraweb068/b2719/ipg.trackyourgraffiticom/index.php:9) in /hermes/bosoraweb068/b2719/ipg.trackyourgraffiticom/includelog.php on line 4 Notice: Undefined index: username in /hermes/bosoraweb068/b2719/ipg.trackyourgraffiticom/includelog.php on line 5 Notice: Undefined index: userid in /hermes/bosoraweb068/b2719/ipg.trackyourgraffiticom/includelog.php on line 6 Warning: Unknown: open(/var/php_sessions/sess_2b20e85a7fb0622645b0db32fe7ac672, O_RDWR) failed: No such file or directory (2) in Unknown on line 0 Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/php_sessions) in Unknown on line 0 Here is also the includelog.php file it refers to. Basically this is the login form you see in the top right corner of webpages. It is supposed to detect if you logged in, then either welcome you or otherwise display a login form. <?php session_start(); error_reporting(E_ALL^ E_NOTICE); $username = $_SESSION['username']; $userid = $_SESSION['userid']; ?> <html> <head> <?php if ($username && $userid) { echo "Welcome <b>$username</b>!"; echo "</br><a href='logout.php'> Logout</a>"; } else { $form = "<div id='form'><form action='login.php' method='post'> <table> <tr> <td>Username:</td> <td><input type='text' name='user'/></td> <td>Password:</td> <td><input type='password' name='password'/></td> <td></td> <td><input type='submit' name='loginbtn' value='Login'/></td> </tr> </table> </form></div>"; echo "$form"; } ?> </head> <body> </body> </html> Edited August 26, 2013 by ryanmetzler3 Quote Link to comment https://forums.phpfreaks.com/topic/281555-session-not-staying-open/#findComment-1446773 Share on other sites More sharing options...
jazzman1 Posted August 26, 2013 Share Posted August 26, 2013 Wow......now get to work! "Google" would be your friend next hours Good spotting scope mac! Quote Link to comment https://forums.phpfreaks.com/topic/281555-session-not-staying-open/#findComment-1446779 Share on other sites More sharing options...
mac_gyver Posted August 26, 2013 Share Posted August 26, 2013 It gave me a whole mess of errors. Here they are rather than just dumping the errors on a forum somewhere, how about reading them and trying to solve this yourself? one of the error messages gives a HUGE HINT - Please verify that the current setting of session.save_path is correct (/var/php_sessions) Quote Link to comment https://forums.phpfreaks.com/topic/281555-session-not-staying-open/#findComment-1446808 Share on other sites More sharing options...
ryanmetzler3 Posted August 27, 2013 Author Share Posted August 27, 2013 rather than just dumping the errors on a forum somewhere, how about reading them and trying to solve this yourself? one of the error messages gives a HUGE HINT - Please verify that the current setting of session.save_path is correct (/var/php_sessions) I was attempting to do it on my own once I saw the errors. I just figured I would post them in case anyone was very familiar with this. For example I could solve this problem in 3 minutes now for someone else instead of the hour it took me. But thanks for the hint. Quote Link to comment https://forums.phpfreaks.com/topic/281555-session-not-staying-open/#findComment-1446923 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.