Link to home
Start Free TrialLog in
Avatar of aej1973
aej1973

asked on

Undefined index in session variable

I am using a session variable named $_SESSION['role'], which I initialize when a user logs on to my application. When I check the value of this variable in one of my later scripts, I get an error message saying 'Undefined index: role in ....'. How do I correct this error?
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aej1973
aej1973

ASKER

Here's what I am doing: based on the user name, I use this statement
session_start();
...
if ($_POST['user_name']=="XYZ") {
$_SESSION['role'] = 1;
header('location: main_menu.html');
}//endif
else {
$_SESSION['role'] = 0;
header ('location: reportchoice.php');
}//end else

In my script:
session_start();
...
<?php
 if (isset($_SESSION['role']))
   { $role = $_SESSION['role']; }
echo 'The value of $_SESSION[\'role\'] is' .$role . '<br />';
if (!isset($_SESSION['role'])) {
?>
<p align="center"><input  type="submit" name="submit" value="Return to Main Menu"></p>
<?php
}
?>

I used session_start() on all pages accessing the session variable. It still gives me the same error.