Solved

Undefined index in session variable

Posted on 2006-06-09
3
713 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 …

636 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