Solved

Undefined index in session variable

Posted on 2006-06-09
3
692 Views
Last Modified: 2010-05-18
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?
0
Comment
Question by:aej1973
3 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 25 total points
ID: 16873780
either,
* check if it is set before using it:
  if (isset($_SESSION['role']))
   { $role = $_SESSION['role']; }

* ignore the fact if it is set:
  $role = @$_SESSION['role'];

* check out WHY it is not set:
  did you use session_start() on all the pages?
0
 
LVL 21

Accepted Solution

by:
Julian Matz earned 25 total points
ID: 16873849
Another way to check if the session is registered:

$role = isset($_SESSION['role']) ? $_SESSION['role'] : '';

The above will allow you to use the $role variable whether the session is registered or not. If the session is not registered, then the $role variable will be empty, but set, so you will not receive an error.
0
 

Author Comment

by:aej1973
ID: 16873880
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now