$_SESSION Variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rgraham45
Forum Newbie
Posts: 5
Joined: Fri May 18, 2007 5:49 am

$_SESSION Variable

Post 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
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

you need to use session_start(); before you can use $_SESSION

session_start(); should be the first thing on your php script.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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().
rgraham45
Forum Newbie
Posts: 5
Joined: Fri May 18, 2007 5:49 am

I do use the sesion start

Post 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');
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your array referencing is incorrect.

$_SESSION['SESSION']['is_admin'] is correct, if the print_r() is accurate.
rgraham45
Forum Newbie
Posts: 5
Joined: Fri May 18, 2007 5:49 am

Post by rgraham45 »

Thanks, that worked, but why? I read tutorials, reviewed examples and never saw how to correctly reference the $_SESSION var
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
rgraham45
Forum Newbie
Posts: 5
Joined: Fri May 18, 2007 5:49 am

Post by rgraham45 »

I see that it is a multi-level array, with the same name. Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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);
?>
rgraham45
Forum Newbie
Posts: 5
Joined: Fri May 18, 2007 5:49 am

Post by rgraham45 »

Thanks
Post Reply