Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Undefined index in session variable

Posted on 2006-06-09
3
Medium Priority
?
717 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 100 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 100 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

722 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