optikalefx Posted February 5, 2009 Share Posted February 5, 2009 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 More sharing options...
Lodius2000 Posted February 5, 2009 Share Posted February 5, 2009 start all of your pages with <?php session_start(); ?> (PUT IT AT THE VERY TOP)//not yelling just emphasising and remove session_start(); from any other places you have it in the pages, then test again, if problem persists let us know Link to comment https://forums.phpfreaks.com/topic/143868-sessions-not-keeping-variables/#findComment-754953 Share on other sites More sharing options...
optikalefx Posted February 5, 2009 Author Share Posted February 5, 2009 That line is in a php file that is included first on every page Link to comment https://forums.phpfreaks.com/topic/143868-sessions-not-keeping-variables/#findComment-754973 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.