Solved

PHP error undefined index

Posted on 2008-06-09
4
249 Views
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), \
2=>ADMIN(edit,view,update)
3=>ANALYST(view only)

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

//----- functions.php -----

<?php

	#require_once('db.php');
 

	//LEVEL ACCESS

	//1=> SYS ADMIN(everything plus DELETE), 2=>ADMIN(edit,view,update) 3=>ANALYST(view only)

	echo("sorry");

	function checkLogin($role)

	{

	echo $role;

	exit;

		if(!$_SESSION['logged_in'])

		{

			$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($val==$row['LEVEL_access'])

				{//if the user level matches one of the allowed levels

					$access = TRUE;

				}

			}

		}

		if($access==FALSE)

		{

			header("Location: login.php");

		}

		else {

		//do nothing: continue

		}

		

	}

?>

Open in new window

0
Comment
Question by:Isaac
4 Comments
 
LVL 17

Expert Comment

by:nanharbison
Comment Utility
In line 10, you echo $role and then in line 11 you have
exit;
so none of the code below that will be executed.
0
 
LVL 5

Author Comment

by:Isaac
Comment Utility
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/serveinc.org/docs/admin/functions.php on line 52

Notice: Undefined index: LEVEL_access in /home/serveinc/serveinc.org/docs/admin/functions.php on line 52

Notice: Undefined index: LEVEL_access in /home/serveinc/serveinc.org/docs/admin/functions.php on line 52

Warning: Cannot modify header information - headers already sent by (output started at /home/serveinc/serveinc.org/docs/admin/functions.php:52) in /home/serveinc/serveinc.org/docs/admin/functions.php on line 60

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

Expert Comment

by:neeraj523
Comment Utility
Add these lines at the top of the page..

<?php
ob_start();
session_start();
?>

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

neeraj523
0
 
LVL 16

Accepted Solution

by:
DrNikon224 earned 500 total points
Comment Utility
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
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 look for a specific file type in a local or remote server directory using PHP.

728 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

9 Experts available now in Live!

Get 1:1 Help Now