$_SESSION Variable
Moderator: General Moderators
$_SESSION Variable
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
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
I do use the sesion start
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');
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');
It's just an array, don't treat it special.
Why do I get Notice: Undefined index: is_admin for the first echo-line while the second prints the value stored in the array?
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";
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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:
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
$user_id = $_SESSION['is_admin'];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);
?>