Jump to content

[SOLVED] $_Session Not Working Not Passing Variable


centerwork

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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