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");	
	}
}
?>

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.

Archived

This topic is now archived and is closed to further replies.

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