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"); } } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted August 4, 2008 Share Posted August 4, 2008 Where do you define $sessVar? Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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.