Page 1 of 1

$_SESSION Variable

Posted: Fri May 18, 2007 6:23 am
by rgraham45
I have tried to understand and read tutorials about accessing the session variables and I can't figure this out.

Here is the code

$user_id = $_SESSION['is_admin'];
print "<p>session var: ";print $user_id;print "<p>session var: <p>";
print_r($_SESSION);


here is the result:

session var:

session var:

Array ( [SESSION] => Array ( [user_id] => 8 [customer_id] => [is_admin] => 1 [first_name] => John [last_name] => Poppe [allow_customize] => 0 ) [enrollment_customer_id] => 9 [enrollment_user_id] => 116 )

$_SESSION['is_admin'] contains a value of 1 when I display the $_SESSION array, but when I try to get the value from the session variable I get nothing

I am missing something here, any ideas, thanks

Posted: Fri May 18, 2007 6:25 am
by blackbeard
you need to use session_start(); before you can use $_SESSION

session_start(); should be the first thing on your php script.

Posted: Fri May 18, 2007 6:52 am
by onion2k
blackbeard wrote:you need to use session_start(); before you can use $_SESSION
That depends on your PHP configuration actually. If session.auto_start is set then you don't need to call session_start().

I do use the sesion start

Posted: Fri May 18, 2007 6:53 am
by rgraham45
session_start();
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');

Posted: Fri May 18, 2007 8:32 am
by feyd
Your array referencing is incorrect.

$_SESSION['SESSION']['is_admin'] is correct, if the print_r() is accurate.

Posted: Fri May 18, 2007 11:59 am
by rgraham45
Thanks, that worked, but why? I read tutorials, reviewed examples and never saw how to correctly reference the $_SESSION var

Posted: Fri May 18, 2007 12:19 pm
by volka
It's just an array, don't treat it special.

Code: Select all

<?php
$arr = array(
	'SESSION' => array(
		'is_admin' => 'true'
	)
);

echo 'is_admin: ', $arr['is_admin'], "<br />\n";
echo 'SESSION.is_admin: ', $arr['SESSION']['is_admin'], "<br />\n";
?>
Why do I get Notice: Undefined index: is_admin for the first echo-line while the second prints the value stored in the array?

Posted: Fri May 18, 2007 12:43 pm
by rgraham45
I see that it is a multi-level array, with the same name. Thanks

Posted: Fri May 18, 2007 6:21 pm
by RobertGonzalez
Somewhere along the line, you created a $_SESSION['SESSION'] array index and assigned it a value of array() that you keep feeding with data. So when you use the line:

Code: Select all

$user_id = $_SESSION['is_admin'];
You are actually getting an undefined index error thrown at you by the parser. You are probably not seeing it become display_errors is off.

Add these lines to the top of your script and run it again. You should see that PHP doesn't like that array index:

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
?>

Posted: Sat May 19, 2007 9:45 am
by rgraham45
Thanks