centerwork Posted August 4, 2008 Share Posted August 4, 2008 I am racking my brain with this one. I think it may have something to do with the header("Location:...") tag. Here is the login accepted code snip it: <?php if($login == 'yes') { session_start(); //Saves information for site. $_SESSION[$sessVar] = ''.$user.''; header("Location: http://".$root_site."/admin/main.php"); } ?> Here is the page code that the header send the user to. The variable is not read by the if statement or and echo farther down on the page: <?php //This script adds new products to the product/service menu. require ($_SERVER["DOCUMENT_ROOT"] . "/ssl/db_config.php"); $connection = mysql_connect($db_host, $db_user, $db_password, $db_name) or die ("error connecting"); mysql_select_db($db_name); //Checks if there is a login cookie if(!isset($_SESSION[$sessVar])) { header("Location: http://".$root_site."/admin/index.php"); } ?> Any help would be appreciated. If you need it here is the full code for the login page: <?php // Connects to your Database require ($_SERVER["DOCUMENT_ROOT"] . "/ssl/db_config.php"); $connection = mysql_connect($db_host, $db_user, $db_password, $db_name)or die ("error connecting"); mysql_select_db($db_name); //Pulls Submited Variables. $submit = $_POST['submit']; $user = $_POST['user']; $pass = $_POST['pass']; $error1=0; $error2=0; $error3=0; $error4=0; $date=time(); $login="yes"; //if the login form is submitted if ($submit=="Login") { // if form has been submitted // makes sure they filled it in if(strlen($user)<=0) { $error1=1; $login="no"; } if(strlen($pass)<=0) { $error2=1; $login="no"; } $query = "SELECT * FROM users WHERE username = '$user'"; $results = mysql_query($query)or die(mysql_error()); $rows = mysql_fetch_array($results); $check2 = mysql_num_rows($results); $user_db = $rows['username']; $pass_db = $rows['password']; //Gives error if user doesn't exist if ($check2 == 0) { $error3=1; $login="no"; } //gives error if the password is wrong if ($pass != $pass_db) { $error4=1; $login="no"; } if($login == 'yes') { session_start(); //Saves information for site. $_SESSION[$sessVar] = ''.$user.''; header("Location: http://".$root_site."/admin/main.php"); } } ?> Link to comment https://forums.phpfreaks.com/topic/118010-solved-_session-not-working-not-passing-variable/ Share on other sites More sharing options...
trq Posted August 4, 2008 Share Posted August 4, 2008 Where do you define $sessVar? Link to comment https://forums.phpfreaks.com/topic/118010-solved-_session-not-working-not-passing-variable/#findComment-607066 Share on other sites More sharing options...
PFMaBiSmAd Posted August 4, 2008 Share Posted August 4, 2008 There is also no session_start(); shown in the 2nd piece of posted code. Link to comment https://forums.phpfreaks.com/topic/118010-solved-_session-not-working-not-passing-variable/#findComment-607073 Share on other sites More sharing options...
centerwork Posted August 4, 2008 Author Share Posted August 4, 2008 Here is the section where the variable is loaded. $_SESSION[$sessVar] = ''.$user.''; And, Do you have to start the session on every page that requests one of the varibles? Link to comment https://forums.phpfreaks.com/topic/118010-solved-_session-not-working-not-passing-variable/#findComment-607100 Share on other sites More sharing options...
trq Posted August 4, 2008 Share Posted August 4, 2008 And, Do you have to start the session on every page that requests one of the varibles? Yes. And this line... $_SESSION[$sessVar] = ''.$user.''; Still, nowhere in this code do you define $sessVar. Link to comment https://forums.phpfreaks.com/topic/118010-solved-_session-not-working-not-passing-variable/#findComment-607102 Share on other sites More sharing options...
centerwork Posted August 4, 2008 Author Share Posted August 4, 2008 I tried adding the session_start(); to the second page. That did not work. I also tried adding $sessVar = 'foo'; to define it. Is that right? Thanks for your patients and your help. Link to comment https://forums.phpfreaks.com/topic/118010-solved-_session-not-working-not-passing-variable/#findComment-607114 Share on other sites More sharing options...
trq Posted August 4, 2008 Share Posted August 4, 2008 I also tried adding $sessVar = 'foo'; to define it. Is that right? That will not persist. The $_SESSION array is just like any other associative array. Instead of using $_SESSION[$sessVar] in all your scripts simply change it to $_SESSION['foo'] and see how you go. Link to comment https://forums.phpfreaks.com/topic/118010-solved-_session-not-working-not-passing-variable/#findComment-607120 Share on other sites More sharing options...
centerwork Posted August 4, 2008 Author Share Posted August 4, 2008 Thank you, That worked just fine. It was the combination of not defining the the $_SESSION[$sessVar] and not having session_start(); at the top of each page. Link to comment https://forums.phpfreaks.com/topic/118010-solved-_session-not-working-not-passing-variable/#findComment-607133 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.