Solved

Undefined index in session variable

Posted on 2006-06-09
3
690 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

758 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

19 Experts available now in Live!

Get 1:1 Help Now