PHP error undefined index

Posted on 2008-06-09
Last Modified: 2013-12-13
Can somebody take a look at my code and see what I'm doing wrong?
I'm trying to create a login page and when I call the funtion "checkLogin()" it bombs on me.
It's supposed to check what level of access the user has.
Below are the roles.
1=> SYS ADMIN(everything plus DELETE), \
3=>ANALYST(view only)

I have an edit page that calls the function like this:
checkLogin('1 2 3')

//----- functions.php -----
	//1=> SYS ADMIN(everything plus DELETE), 2=>ADMIN(edit,view,update) 3=>ANALYST(view only)
	function checkLogin($role)
	echo $role;
			$access = FALSE;
		else {
			$kt = split(' ', $role);
			$query = mysql_query('SELECT level_access FROM tblUsers WHERE ID = "'. mysql_real_escape_string($_SESSION['user_id']).'"');
			$row = mysql_fetch_assoc($query);
			$access = FALSE;			
 while(list($key,$val)=each($kt))  <-- ERROR LINE
				{//if the user level matches one of the allowed levels
					$access = TRUE;
			header("Location: login.php");
		else {
		//do nothing: continue

Open in new window

Question by:Isaac
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
LVL 17

Expert Comment

ID: 21747815
In line 10, you echo $role and then in line 11 you have
so none of the code below that will be executed.

Author Comment

ID: 21747945
Please ignore lines 10 and 11.  That was just for debugging purposes.

Here's the error I get

Notice: Undefined index: LEVEL_access in /home/serveinc/ on line 52

Notice: Undefined index: LEVEL_access in /home/serveinc/ on line 52

Notice: Undefined index: LEVEL_access in /home/serveinc/ on line 52

Warning: Cannot modify header information - headers already sent by (output started at /home/serveinc/ in /home/serveinc/ on line 60

Line 52 refers to the While Loop and Line 60 is 'header("Location: login.php");'
LVL 22

Expert Comment

ID: 21747983
Add these lines at the top of the page..


Make sure these are the first lines on the page..

LVL 16

Accepted Solution

DrNikon224 earned 500 total points
ID: 21748138
You're seeing the Notice probably because you have error_reporting(E_ALL); (or an equivalent thereof) in your code somewhere. It's not an error really, just a notification that you're making reference to an array index which you've not defined. Specifically, the index "LEVEL_access" - which is not the same as "level_access" which is what you've used in your SELECT query. Case sensitive. If the table field is "level_access" then you'll need to use $row['level_access'].

The "Notice" messages can be disabled (though resolving the issue triggering the Notice is better) - they are not fatal errors.

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

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.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

691 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