n3wbi3 Posted March 2, 2009 Share Posted March 2, 2009 Long story short: I have a php application using DB2 as a database back end. Im new php sessions this is what I am doing -- index.php -- <?php session_start(); require "login.php"; require "header.php"; if ( $_GET['section'] ) { $function=$_GET['section']; $_SESSION['section']=$function; } print ($_SESSION["valid_user"]); ?> <center> <?php verify(); echo session_id(); print_r($_SESSION); ?> -- header.php -- <?php session_start(); echo session_id(); print_r($_SESSION); ?> == What I am noticing is that while the session data shows up in the main body of index.php but not in the header... ever.. I also have a link pointing to another page (resources.php) which carries over the session id but not the session data.. Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/ Share on other sites More sharing options...
revraz Posted March 2, 2009 Share Posted March 2, 2009 Do you have error display and reporting turned on? Are you receiving any Header errors? Is your session save path valid? Are the files accessed via relative links and not absolute? Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-774956 Share on other sites More sharing options...
n3wbi3 Posted March 2, 2009 Author Share Posted March 2, 2009 Do you have error display and reporting turned on? Are you receiving any Header errors? Is your session save path valid? Are the files accessed via relative links and not absolute? Relative links, as to the session paths and error reporting... Im kind of new at this.. where do I set up the path? Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-774990 Share on other sites More sharing options...
revraz Posted March 3, 2009 Share Posted March 3, 2009 The php.ini file stores the session save path. Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-775503 Share on other sites More sharing options...
n3wbi3 Posted March 3, 2009 Author Share Posted March 3, 2009 The php.ini file stores the session save path. Yes the session save path is valid, I am linking via local reference (a href="foo.php")... I have tinkered with this fo a couple of days and in some of my meager debugging I have discovered this: the index file is goes like this (pseudocode) import header.php import login.php import body.php header (right now) just has this start_session pring session id print_r $_SESSION body has this start_session pring session id print_r $_SESSION verify() -- verify just check to see if its a valid user and if it is not it prints up the login screen. Going to the page initially I get the log in screen, and when I log in I see this SessionID=1asdf2, Array () <=== from the header SessionID=1asdf2, Array (login=bob) <=== from the body Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-775524 Share on other sites More sharing options...
HuggieBear Posted March 3, 2009 Share Posted March 3, 2009 Why are you using session_start() in both the header and the body? Just put it in the header and be done with it. Make sure it's the first thing you do on that page before outputting anything else. Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-775529 Share on other sites More sharing options...
PFMaBiSmAd Posted March 3, 2009 Share Posted March 3, 2009 You are including/requiring the header.php code before the code that sets the session variables is executed. At the point that the code in header.php is executed, the session variable(s) have not been set yet. Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-775530 Share on other sites More sharing options...
n3wbi3 Posted March 3, 2009 Author Share Posted March 3, 2009 You are including/requiring the header.php code before the code that sets the session variables is executed. At the point that the code in header.php is executed, the session variable(s) have not been set yet. I get that but when I follow a link on the page to another with basically the same format header.php body2.php I get a blank array and the login screen again (I run verify on very page) Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-775541 Share on other sites More sharing options...
PFMaBiSmAd Posted March 3, 2009 Share Posted March 3, 2009 If you are getting the exact same session id on the two different pages but no $_SESSION data, it either means your code contains a logic error that is overwriting the $_SESSION variables or you have register_globals on and have same name session/post/get/cookie or program variable names and register_globals is clearing the session variables. You would need to post you actual code, including any functions like verify() for anyone to be able to help you with what you code could be doing that is causing the symptom and what does a phpinfo() statement show for register_globals? Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-775549 Share on other sites More sharing options...
n3wbi3 Posted March 3, 2009 Author Share Posted March 3, 2009 If you are getting the exact same session id on the two different pages but no $_SESSION data, it either means your code contains a logic error that is overwriting the $_SESSION variables or you have register_globals on and have same name session/post/get/cookie or program variable names and register_globals is clearing the session variables. You would need to post you actual code, including any functions like verify() for anyone to be able to help you with what you code could be doing that is causing the symptom and what does a phpinfo() statement show for register_globals? Well register_globals = Off, so I will provide as much code as I can == index2.php == <?php session_start(); echo session_id(); print_r($_SESSION); require "login.php"; require "header.php"; if ( $_GET['section'] ) { $function=$_GET['section']; $_SESSION['section']=$function; } print ($_SESSION["valid_user"]); verify(); if(isset($_SESSION['valid_user'])) { require "left_bar.php"; if ( $_SESSION['section'] ) { if ( $_SESSION['section'] = "resources" ) { //require "Resources.php"; } } require "main.php" ?> <table width="100%" border="0" cellspacing="20" cellpadding="4"> <tr> <td></td> </tr> <tr> <td></td> </tr> </table> </div> <!-- end of maincontent --> <?php require "footer.php"; ?> == login.php === <?php //########## Check first for a log-out request so that a new session can be started ################ if (isset($_POST['log_out'])) { session_unregister( "valid_user" ); session_destroy(); session_start(); } function write_log_in( $text ) { echo " <br><br><br> <table border=\"1\"><tr><td> <center> <p>$text</p> </center> <form method='post' action=''> <center> <table border=\"0\"> <tr><td> User ID:</td> <td> <input type='text' name='user_name' /></td></tr> <tr><td> Password:</td> <td> <input type='password' name='password' /></td></tr> </table> </center> <center> <p><input type='submit' value='Log In'></p> </center> </form> </td></tr></table> "; } // end write_log_in function //############################ Function to logout ###################### function log_out() { echo " <form method='post' action=''> <center> <INPUT TYPE=hidden NAME=log_out VALUE=\"1\"> <p><input type='submit' value='Log Out'></p> </center> </form> "; } //############################ Verify the session login information on every page! ###################### function verify() { // See if this session already includes a valid user if(isset($_SESSION["valid_user"])) { return true; } else { $user_name = $_POST["user_name"]; $password = $_POST["password"]; if ( $user_name && $password ) { // verify password and log in to database $database='MyDB'; $hostname='localhost'; $port = 50000; $username = $user; $_SESSION["hostname"] = $hostname; $_SESSION["database"] = $database; $_SESSION["user_name"] = $user_name; $_SESSION["password"] = $password; $_SESSION["port"] = $port; echo session_id(); print_r($_SESSION); $conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database; HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;UID=$user_name;PWD=$password"; $conn = db2_connect($conn_string, '', ''); if ( $conn ) { $valid_user = $user_name; $_SESSION['valid_user'] = $valid_user; db2_close($conn); return true; } else { $text = "User Name and Password did not match"; echo "$text"; $text = "Welcome to IED-Infrastructure Engineering Department"; write_log_in( $text ); db2_close($conn); } } else { $text = "Welcome to IED-Infrastructure Engineering Department"; } } } ?> == Header.php == nothing but html at this point == leftbar.php == <div id="leftcontent"> <!-- Left Contents --> <table> <tr><td><h1>Contents</h1></td></tr> <tr><th>HOME</th></tr> <tr><td><a href="?section=projects" >Projects</a></td></tr> <tr><td><a href="resources.php" >Resources</a></td></tr> <tr><td><a href="?section=reports" >Reports</a></td></tr> <tr><td> </td></tr> <tr><td> </td></tr> <tr><td><h1>Quick Access</h1></td></tr> <?php if(!isset($_SESSION['valid_user'])) { echo ("<tr><th>Access online?</th></tr>"); echo ("<tr><td ><a href='login.php' ><img src='images/signon.gif' width='64' height='22' alt='Login' /></a></td></tr>"); echo ("<tr><td> </td></tr>"); echo ("<tr><th>Set up online?</th></tr>"); echo ("<tr><td ><a href='registration.php' ><img src='images/register.gif' width='68' height='22' alt='Registration' /></a></td></tr>"); echo ("<tr><td> </td></tr>"); } else { echo ("<tr><th>Change password?</th></tr>"); echo ("<tr><td><a href='External page'>click here</a></td></tr>"); echo ("<tr><td> </td></tr>"); echo ("<tr><td>"); log_out(); echo ("</tr></td>"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-775556 Share on other sites More sharing options...
HuggieBear Posted March 3, 2009 Share Posted March 3, 2009 What are you trying to achive. I'm getting the result I'd expect from the code you posted. Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-775567 Share on other sites More sharing options...
PFMaBiSmAd Posted March 3, 2009 Share Posted March 3, 2009 A) Please post code for each file using the forum's tags. B) After you post the code so that it is readable and you can tell which code is part of which file, things like the following set the $_SESSION variable to the value and are not comparing the variable to the value - if ( $_SESSION['section'] = "resources" ) One = is an assignment operator. Two == is an equal comparison. Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-775569 Share on other sites More sharing options...
n3wbi3 Posted March 3, 2009 Author Share Posted March 3, 2009 What are you trying to achive. I'm getting the result I'd expect from the code you posted. Im trying to go to a second page (sorry I forgot to post it, here it is) <?php session_start(); echo session_id(); print_r($_SESSION); require "login.php"; require "header.php"; //if ( $_GET['section'] ) //{ //$function=$_GET['section']; //$_SESSION['section']=$function; //} // //print ($_SESSION["valid_user"]); ?> <center> <?php verify(); { //require "dbResource.php"; require "left_bar.php"; //require "current.php"; session_start(); function ResourceMain() { echo "<center><table border=\"1\" cellpadding=\"10\" bgcolor=\"white\"><tr> <td width=\"100\"><a href=\"set_main.php?ResTask=createResources\"><b>CreateResources</b></a></td> <td width=\"100\"><a href=\"set_main.php?ResTask=updateResources\"><b>UpdateResources</b></a></td> <td width=\"100\"><a href=\"set_main.php?ResTask=removeResources\"><b>RemoveResources</b></a></td> <td width=\"100\"><a href=\"set_main.php?ResTask=listResources\"><b>ListResources</b></a></td> </tr></table></center>"; } //function viewManagers() //{ echo "</center>"; $managers=dbquery( "select * from iepp01.manager"); $i=0; $resultSize=count($managers); echo "<br/><br/>"; echo "<ul>"; while ( $i < $resultSize) { print ("<li><b>".$managers["$i"]['1']."</b> <a href=\"set_main.php?ResTask=viewManagersEmps?ManID=$i\">View Employees</a></li>"); $i += 1; } echo "</ul>"; //} ResourceMain(); } ?> -- without having to log in again, the session stays the same but all vars are lost fo verify(); fails Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-775570 Share on other sites More sharing options...
n3wbi3 Posted March 3, 2009 Author Share Posted March 3, 2009 A) Please post code for each file using the forum's tags. B) After you post the code so that it is readable and you can tell which code is part of which file, things like the following set the $_SESSION variable to the value and are not comparing the variable to the value - if ( $_SESSION['section'] = "resources" ) One = is an assignment operator. Two == is an equal comparison. A) Sorry, will do.. == index2.php == <?php session_start(); echo session_id(); print_r($_SESSION); require "login.php"; require "header.php"; if ( $_GET['section'] ) { $function=$_GET['section']; $_SESSION['section']=$function; } print ($_SESSION["valid_user"]); verify(); if(isset($_SESSION['valid_user'])) { require "left_bar.php"; if ( $_SESSION['section'] ) { if ( $_SESSION['section'] = "resources" ) { //require "Resources.php"; } } require "main.php" ?> <table width="100%" border="0" cellspacing="20" cellpadding="4"> <tr> <td></td> </tr> <tr> <td></td> </tr> </table> </div> <!-- end of maincontent --> <?php require "footer.php"; ?> == login.php === <?php //########## Check first for a log-out request so that a new session can be started ################ if (isset($_POST['log_out'])) { session_unregister( "valid_user" ); session_destroy(); session_start(); } function write_log_in( $text ) { echo " <br><br><br> <table border=\"1\"><tr><td> <center> <p>$text</p> </center> <form method='post' action=''> <center> <table border=\"0\"> <tr><td> User ID:</td> <td> <input type='text' name='user_name' /></td></tr> <tr><td> Password:</td> <td> <input type='password' name='password' /></td></tr> </table> </center> <center> <p><input type='submit' value='Log In'></p> </center> </form> </td></tr></table> "; } // end write_log_in function //############################ Function to logout ###################### function log_out() { echo " <form method='post' action=''> <center> <INPUT TYPE=hidden NAME=log_out VALUE=\"1\"> <p><input type='submit' value='Log Out'></p> </center> </form> "; } //############################ Verify the session login information on every page! ###################### function verify() { // See if this session already includes a valid user if(isset($_SESSION["valid_user"])) { return true; } else { $user_name = $_POST["user_name"]; $password = $_POST["password"]; if ( $user_name && $password ) { // verify password and log in to database $database='MyDB'; $hostname='localhost'; $port = 50000; $username = $user; $_SESSION["hostname"] = $hostname; $_SESSION["database"] = $database; $_SESSION["user_name"] = $user_name; $_SESSION["password"] = $password; $_SESSION["port"] = $port; echo session_id(); print_r($_SESSION); $conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database; HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;UID=$user_name;PWD=$password"; $conn = db2_connect($conn_string, '', ''); if ( $conn ) { $valid_user = $user_name; $_SESSION['valid_user'] = $valid_user; db2_close($conn); return true; } else { $text = "User Name and Password did not match"; echo "$text"; $text = "Welcome to IED-Infrastructure Engineering Department"; write_log_in( $text ); db2_close($conn); } } else { $text = "Welcome to IED-Infrastructure Engineering Department"; } } } ?> == Header.php == nothing but html at this point == leftbar.php == <div id="leftcontent"> <!-- Left Contents --> <table> <tr><td><h1>Contents</h1></td></tr> <tr><th>HOME</th></tr> <tr><td><a href="?section=projects" >Projects</a></td></tr> <tr><td><a href="resources.php" >Resources</a></td></tr> <tr><td><a href="?section=reports" >Reports</a></td></tr> <tr><td> </td></tr> <tr><td> </td></tr> <tr><td><h1>Quick Access</h1></td></tr> <?php if(!isset($_SESSION['valid_user'])) { echo ("<tr><th>Access online?</th></tr>"); echo ("<tr><td ><a href='login.php' ><img src='images/signon.gif' width='64' height='22' alt='Login' /></a></td></tr>"); echo ("<tr><td> </td></tr>"); echo ("<tr><th>Set up online?</th></tr>"); echo ("<tr><td ><a href='registration.php' ><img src='images/register.gif' width='68' height='22' alt='Registration' /></a></td></tr>"); echo ("<tr><td> </td></tr>"); } else { echo ("<tr><th>Change password?</th></tr>"); echo ("<tr><td><a href='External page'>click here</a></td></tr>"); echo ("<tr><td> </td></tr>"); echo ("<tr><td>"); log_out(); echo ("</tr></td>"); } ?> ==resources.php== <?php session_start(); echo session_id(); print_r($_SESSION); require "login.php"; require "header.php"; //if ( $_GET['section'] ) //{ //$function=$_GET['section']; //$_SESSION['section']=$function; //} // //print ($_SESSION["valid_user"]); ?> <center> <?php verify(); { //require "dbResource.php"; require "left_bar.php"; //require "current.php"; session_start(); function ResourceMain() { echo "<center><table border=\"1\" cellpadding=\"10\" bgcolor=\"white\"><tr> <td width=\"100\"><a href=\"set_main.php?ResTask=createResources\"><b>CreateResources</b></a></td> <td width=\"100\"><a href=\"set_main.php?ResTask=updateResources\"><b>UpdateResources</b></a></td> <td width=\"100\"><a href=\"set_main.php?ResTask=removeResources\"><b>RemoveResources</b></a></td> <td width=\"100\"><a href=\"set_main.php?ResTask=listResources\"><b>ListResources</b></a></td> </tr></table></center>"; } //function viewManagers() //{ echo "</center>"; $managers=dbquery( "select * from iepp01.manager"); $i=0; $resultSize=count($managers); echo "<br/><br/>"; echo "<ul>"; while ( $i < $resultSize) { print ("<li><b>".$managers["$i"]['1']."</b> <a href=\"set_main.php?ResTask=viewManagersEmps?ManID=$i\">View Employees</a></li>"); $i += 1; } echo "</ul>"; //} ResourceMain(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-775576 Share on other sites More sharing options...
n3wbi3 Posted March 3, 2009 Author Share Posted March 3, 2009 just bumping this up because I am at my wits end.. Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-775722 Share on other sites More sharing options...
revraz Posted March 3, 2009 Share Posted March 3, 2009 Again, session_start() needs to be the first line of code in every file you want to use sessions in, and you can't destroy a session until it's started. (Doesn't have to be first, but has to be before any output to the browser so just make it the first) Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-775798 Share on other sites More sharing options...
n3wbi3 Posted March 3, 2009 Author Share Posted March 3, 2009 Again, session_start() needs to be the first line of code in every file you want to use sessions in, and you can't destroy a session until it's started. (Doesn't have to be first, but has to be before any output to the browser so just make it the first) All the other php files are brought in via require, I did have the session_start() in all of them until someone told me not to keep them all Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-775833 Share on other sites More sharing options...
n3wbi3 Posted March 4, 2009 Author Share Posted March 4, 2009 bumping to top because I am still stuck.. Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-776433 Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2009 Share Posted March 4, 2009 So, have you tested if sessions are working at all using two simple files that both have a session_start() statement and set a session variable in the first page and echo it in the second page? You never really provided information if the session_id you were seeing on the different pages is the same id or different ones or if you ever checked or got full error reporting turned on. Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-776453 Share on other sites More sharing options...
n3wbi3 Posted March 4, 2009 Author Share Posted March 4, 2009 So, have you tested if sessions are working at all using two simple files that both have a session_start() statement and set a session variable in the first page and echo it in the second page? You never really provided information if the session_id you were seeing on the different pages is the same id or different ones or if you ever checked or got full error reporting turned on. Yes, when I run th eprogram the session ID stays the same but the $_SESSION array seems to drop off hte face of the earth.. Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-776473 Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2009 Share Posted March 4, 2009 Then something in your index.php (first file) after the point where you do print_r($_SESSION) or in your index2.php (second file) before the point where you do a print_r($_SESSION) is clearing the $_SESSION variables. Your logout code has a session_destroy();. Any change you have another one in any the code you have not posted? Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-776477 Share on other sites More sharing options...
n3wbi3 Posted March 4, 2009 Author Share Posted March 4, 2009 Then something in your index.php (first file) after the point where you do print_r($_SESSION) or in your index2.php (second file) before the point where you do a print_r($_SESSION) is clearing the $_SESSION variables. Your logout code has a session_destroy();. Any change you have another one in any the code you have not posted? nope what you see is what I have Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-776482 Share on other sites More sharing options...
n3wbi3 Posted March 4, 2009 Author Share Posted March 4, 2009 Then something in your index.php (first file) after the point where you do print_r($_SESSION) or in your index2.php (second file) before the point where you do a print_r($_SESSION) is clearing the $_SESSION variables. Your logout code has a session_destroy();. Any change you have another one in any the code you have not posted? nope what you see is what I have bump, plesae help Quote Link to comment https://forums.phpfreaks.com/topic/147603-help-with-session-variables/#findComment-776651 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.