Link to home
Start Free TrialLog in
Avatar of Isaac
IsaacFlag for United States of America

asked on

PHP error undefined index

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

Avatar of nanharbison
nanharbison
Flag of United States of America image

In line 10, you echo $role and then in line 11 you have
exit;
so none of the code below that will be executed.
Avatar of Isaac

ASKER

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");'
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
ASKER CERTIFIED SOLUTION
Avatar of GarbsTheTurtle
GarbsTheTurtle
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial