Jump to content

session not staying open


Go to solution Solved by mac_gyver,

Recommended Posts

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 by ryanmetzler3
Link to comment
https://forums.phpfreaks.com/topic/281555-session-not-staying-open/
Share on other sites

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. 

  • Solution

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);

 

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 by ryanmetzler3
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)

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.