Jump to content

Sessions not keeping variables


optikalefx

Recommended Posts

This is really bothering me.  I think I don't know something about sessions.

I have 5 php pages and a login php file.  When you login it creates a session and stores 3 variables. first name last name and both together

 

part of login.php

if ($emailFound) {
if ($passFound) {
	session_start();
	$res = mysql_query("SELECT * FROM customerInfo WHERE email = '$email'") or die(mysql_error());  
	while($row = mysql_fetch_array( $res )) {
		$_SESSION['fName'] = $row['fName'];
		$_SESSION['lName'] = $row['lName'];
		$_SESSION['name'] = $_SESSION['fName'] . " " .$_SESSION['lName'];
	}
	header("Location: index.php");
} else header("Location: login.php?err=".WRONGPASS);
} else header("Location: login.php?err=".EMAIL);

 

Then on ever page I have at the very very top included a file called session.php

 

at the top of every viewable page

<?PHP include('session.php');?>

 

contents of session.php

<?PHP
if( isset( $_COOKIE['PHPSESSID'] ) ) {
session_start();
$name = $_SESSION['name'];
$lgn = "Welcome back ".$name."<br><a id='namez' href='http://www.mysite.com/logout.php'>Not ".$name."?</a>";
} else {
$lgn = "<a id='namez' href='http://www.mysite.com/login.php'>Login</a>";
}

?>

 

And then later on i include a file that displays the info from the session

<?PHP include('top.php');?>

 

and inside of that file

<div id="namez"><?=$lgn?></div>

 

It works just fine for /index.php /page1.php and /page2.php

but it fails for /dir/page.php and /dir/page2.php

Even though those includes are both

<?PHP include('../session.php');?>
//and
<?PHP include('../top.php');?>

 

The issue is that with the pages that are a directory deep, the SOMETIMES work.  And its not always the same ones.  So i cant draw a conclusion as to why this is happening.  if they were always not working, ok i can fix it.  But they are sometimes working.

 

MOREOVER, its passing the isset for the cookie just fine on every page everytime.  Its just forgetting the $_SESSION['name'] variable at times.  Sometimes its there, sometimes it just says "Weclome back, Not ?"

 

Any ideas what Im missing in all this? Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/143868-sessions-not-keeping-variables/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.