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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.