Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

What does this error mean?

Here's the error I'm getting:

 Notice: Undefined variable: main_control in /mnt/vhosts/heavydutylighting.com/httpdocs/cms/index.php on line 124 Notice: Undefined variable: main_control in /mnt/vhosts/heavydutylighting.com/httpdocs/cms/index.php on line 158 ACCESS DENIED control=

From what I've been able to determine so far, the main_control shows up initially at 107 and that seems to be dependent on $control.

In any event, I've spent a bunch of time just getting the site to talk to the correct database. Now that that is in place, I'm running into this mess and I'm not sure what the problem is or how to fix it.

Thoughts?

page is attached
index.php
SOLUTION
Avatar of Ray Paseur
Ray Paseur
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
$main_control is initialized on line 107.  It is an array made up from explode(), called on a variable $control.

$control is set on line 17, and possibly modified on lines 23 and/or 27, depending on the request elements in $_GET or $_POST.  $control may be used on line 105.

Not to be flip, but is there any way you can just get rid of this?  It looks like very amateurish programming, and I am not sure it would be worth the time to try to debug it.  But if you want to debug it, the first thing I would recommend is to add error_reporting(E_ALL) to the top of the script and then start using var_dump() to print out the independent variables.  Via that technique you can see where values are coming from and what variables are used to create other variables.  It's not an easy process, and in this pile of spaghetti it may take quite a while to get to a good answer, sorry.
Avatar of Bruce Gust

ASKER

Morning, Ray! Glad you're up and at 'em!

Frankly, while I'm always open to the path of least resistance, I feel like out of respect for the fact that this isn't my code, I need to try and fix what's there before I introduce something of my own.

So, if you're willing, here goes:

I put error_reporting(E_ALL) at the top of the page and nothing else besides what I've already documented came up. I then added a var_dump($control); on line 70 to see if that was being "seen" at all, and it doesn't appear to be.

I can't figure out where $control is coming from...
<?php
error_reporting(E_ALL);
 include('systemclient.inc');include('mod_user/user.inc.php');include('mod_user/group.inc.php');include('mod_user/editself.inc.php');include('mod_image/image.inc.php');include('obj_pdf/pdffile.inc.php');include('mod_gallery/gallery.inc.php');include('obj_singleimage/singleimage.inc.php');include('mod_product/product.inc.php');include('mod_rep/rep.inc.php');session_start();
ini_set("display_errors",1);

// MAIN APPLICATION CONTROL //
// this coding is the main switch for the application logic
//it controls security clearence and all requests are made to this switch

//Include the Config File//
/*
the config file contains the location information for the main div elements
*/
include 'config.php';
//include('stepsequence.inc.php');

// set the app wide control value to a default start value
$control="main.null.null";
$noparts=0;

//place any form or url  control variable into the local varaible
//print_r($_POST);exit;
if(isset($_POST["control"])){
	$control=$_POST["control"];
}

if(isset($_GET["control"])){
	$control=$_GET["control"];
}

if(isset($_POST["noparts"])){
	$noparts=$_POST["noparts"];
}

if(isset($_GET["noparts"])){
	$noparts=$_GET["noparts"];
}



/* FUNCTIONS */
include 'master_functions.php';


/* Initilize the application*/


$_SESSION["editsteps"] = array();

// if a url var of logout is set then delete current client information
if(isset($_GET["logout"])){
	$_SESSION["SystemClient"] = "";
	$_SESSION ="";
	}
//this controls the timeout length of the client session
if(isset($_SESSION["timeout"])){
	$diff = time()*1 - $_SESSION["timeout"];
	if($diff > 1800){
		$_SESSION["SystemClient"] = "";
		$_SESSION["timeout"] = time();
	}else{
		$_SESSION["timeout"]=time();
	}
}else{
	$_SESSION["timeout"]=time();
}
//call the DataRecord Function//
/*its purpose is to save all form values into their respective session objects*/
StoreData();
var_dump($control);
	?>
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
	<html>
	<head>
	<title><?php echo $_SESSION['layout']['company_name']; ?> CMS</title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	<script src="default.js" type="text/javascript"></script>
	<script src="utilities.js" type="text/javascript"></script>
	<script src="cal2.js" type="text/javascript"></script>
	<script src="cal_conf2.js" type="text/javascript"></script>
	<?php include 'style.php'?>
</head>

<body style="margin:0px;">
<?php include "login_script.php"; ?>
		<div style="width:100%;height:4;background-color:<?php echo $_SESSION['color_01']; ?>;position:absolute;top:0px;left:0px;border-bottom:1px solid black;z-index:0;"></div>
		<div class="div_control">
			<?php 
			//ECHO for debug
				if(!$noparts){
				echo $control;
				}
			?>
		</div>
		<div class="div_header">
			<img src="media/cms_header.gif" alt="
				<?php $_SESSION['layout']['company_name']?> CMS" 
				<?php
					$name=$_SESSION['SystemClient']->GetName();
					if (strpos($_SERVER['SERVER_NAME'],"localhost") && trim(strtoupper($name))=="MASTERa  CONTROL"){
						echo "width=\"1\"";
					}?> 
			height="69">
		</div>
		<?php
		if(is_object($_SESSION["SystemClient"]) && $_SESSION["SystemClient"]->HasAccess($control)){ 
			//content switch will go here
			$main_control = explode(".", $control);
			include 'nav.php';
			echo"<div style=\"position:absolute;top:100px;height:22px;left:0px;\">";
			BuildNav($nav,$control);
			echo"</div>";
			//always display the logout option
			//615
			DisplayLogout(625,6,$_SESSION["SystemClient"]->GetName());
		}
		?>
		<?php
			$div_height=153 + $_SESSION['main_nav_height'];
		?>
		<div class="div_main_holder" style="top:<?php echo $div_height ?>px;position:absolute;left:15px;">
		
		<?php

			switch ($main_control[0]) {
			
				//DO NOT REMOVE THESE CASES:THEY ARE THE DEFAULT INSTALLED CASES
				case "editself":
					//include the main switch for the user controls
					include 'mod_user/editself.php';
					break;
				case "user":
					//include the main switch for the user controls
					include 'mod_user/index.php';
					break;
				 case "group":
					//include the main switch for the user controls
					include 'mod_user/group_index.php';
					break;
				case "product":
					//include the main switch for the user controls
					include 'mod_product/index.php';
					break;
				case "image":
					//include the main switch for the user controls
					include 'mod_image/index.php';
					break;
				case "main":
					//include the main switch for the user controls
					include 'main.php';
					break;
				case "rep":
					include 'mod_rep/index.php';
					break;

				//END DEFAULT CASES//
			
				default:
					print "<span style=\"COLOR:RED;font-size:34px;\">ACCESS DENIED control=".$main_control[0]."</span>";
					break;
			}
		?>
		</div>

</body>
</html>

Open in new window

Hey, mate.  $control is getting set from the HTTP request -- either via POST['control'] or GET['control'] with the GET URL variable overwriting whatever was in the POST.  See line 18, 23-24 and 27-28.  So my guess is that this script expects something from an external source.

Maybe you can sprinkle some var_dump() statements into the script to try to follow the variables that way.  That is what I would do.

Sure would have been nice if the original author had given us some comments.
SOLUTION
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
Gentlemen, I value the wisdom being disseminated here. If my best bet is to eliminate the dynamic that's causing the problem, I'm open to whatever represents a better approach. The long and short of it is, I'm not sure what he's doing. My guess is that the menu options are not displayed without a correct $control variable. But I don't see how or where I make that a moot point.

I'm not interested in others doing the work for me, but if anyone is willing to point me in the right direction, I'm willing to purchase them a digital Diet Coke...
Try putting ?control=FOO in the URL when you run this script.  See what it does.  Perhaps "FOO" will show up somewhere in the output and it may give a clue to what is going on.  I wish I could point you in the right direction, but I have no way of knowing what direction that might be!

We have over 100 lines of code here, and there are a great many include statements for scripts that we do not have (I counted 22, but I may have missed some).  It's more of a project for someone who can get hands-on into the code and data base and spend a few hours injecting test elements.
SOLUTION
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
brucegust,

Slick812's comment may have identified one of the most important issues here.  Even if it isn't it needs to be changed as it will most probably cause problems down the road.  The
session_start(); function must be the first line in the .php script file.

Aielloj
What Aielloj said about the session_start() function.  But that aside, your quote, "I'm not sure what he's doing." might be telling.  From the look of the code, I would guess that the author was not sure what he was doing, either.  Putting session_start() at the top of the script is kind of PHP-101.  And who knows... It may actually be at the top of the script because it could be present in any of those other include() files.  PHP will only raise a Notice if the script calls session_start() more than once, and by default the PHP Notice messages are suppressed.  To see the Notices it is necessary to raise the error_reporting() level.
I think I've found something. This may not be THE problem, but at least I think I'm unravelling  a little bit of the spaghetti...

The error right now is that $main_control is undefined. $main_control comes from $control and that's what I've been searching for and I think I just now found it, but I'm still not out of the dark and would love some input.

Below is the systemclient.inc file. Within that file is a function called SystemClient which is referenced on line 106 of the index.php file. This is the ball of snot that comes up empty where the $main_control variable is concerned...

      <?php
            if(is_object($_SESSION["SystemClient"]) && $_SESSION["SystemClient"]->HasAccess($control)){
                  //content switch will go here
                  $main_control = explode(".", $control);
                  include 'nav.php';
                  echo"<div style=\"position:absolute;top:100px;height:22px;left:0px;\">";
                  BuildNav($nav,$control);
                  echo"</div>";
                  //always display the logout option
                  //615
                  DisplayLogout(625,6,$_SESSION["SystemClient"]->GetName());
            }
            ?>

On line 31 of the systemclient.inc file is a query that's popping the hood of two tables: users and access. One of the column names in the access table is "control" and I'm thinking that's where the problem is.

Take a look at the systemclient.inc file and tell me if I'm barking up the right tree...
<?php

/*This is the 'SystemClient' object
It will store infromation about a current system client in it's object structure
 */
 
 
 class SystemClient{
 	
	var $id, $name,$username,$email,$privileges,$group_type,$group_id;
	
		
 	function SystemClient ($user_id){
		if (strpos($_SERVER['SERVER_NAME'],"snowdogweb")){
		$user="seth";
		$pass="newguy";
		$datasource="heavy_duty";
		$server="localhost";
		}else{
		$user = "hdldba";
		$dbase_user="hdldba";
		$pass="";
		$datasource = "hdlighting_";
		$server="localhost";
		}
		
			
		$dbase_user = mysql_connect($server,$user,$pass);
		mysql_select_db($datasource, $dbase_user);
	
		$query_results = mysql_query("SELECT users.id,users.first_name,users.last_name,users.m_inital,users.email,users.username,groups.type,users.group_id from users LEFT JOIN groups ON users.group_id = groups.id WHERE users.id='$user_id'", $dbase_user);
		
		$data = mysql_fetch_array($query_results);
		//print_r($data);
		$this->id = $data["id"];
		$this->name = $data["first_name"] ." ". $data["m_inital"]  ." ". $data["last_name"];
		$this->username = $data["username"];
		$this->email = $data["email"];
		$this->group_type = $data["type"];
		$this->group_id=$data["group_id"];
	}
 
 	function NumPriv(){
	
	return count($this->privileges);
	}	
	
	function ReturnGroupId(){
		return $this->group_id;
	}	
	
	function Privileges(){ //uses the user id carried in the session
		$user_id = $this->id;
		//echo $user_id;
		if (strpos($_SERVER['SERVER_NAME'],"snowdogweb")){
		$user="seth";
		$pass="newguy";
		$datasource="heavy_duty";
		$server="localhost";
		}else{
		$user = "hdldba";
		$dbase_user="hdldba";
		$pass="";
		$datasource = "hdlighting_";
		$server="localhost";
		}

		$dbase_user = mysql_connect($server,$user,$pass);
		mysql_select_db($datasource, $dbase_user);
	
		$query_results = mysql_query("SELECT access.control, access.has_access from users LEFT JOIN groups ON (users.group_id = groups.id) LEFT JOIN access ON (groups.id = access.group_id) WHERE users.id='$user_id' and access.has_access", $dbase_user);
	
		$this->privileges =  array();
		
		while($row = mysql_fetch_row($query_results)) { 
			$this->privileges[$row[0]] = $row[1];
			$parts= explode(".",$row[0]);
			if(!array_key_exists($parts[0],$this->privileges)){
				$this->privileges[$parts[0]] = 1;
			}
		} 

	}
	
	function HasAccess ($the_control){ //checks to see if user has access to control
		if(array_key_exists($the_control, $this->privileges)){
			return true;
		}else if($this->group_type == "master"){
			return true;
		}else{
			return false;
		} 
	}
	function GetId(){
	
		return $this->id;
	}

	function GetName(){
		return $this->name;
	
	}
	
} 
 ?>

Open in new window

BTW: The $pass variable has been intentionally omitted.
Sigh.  

Please see lines 21-30 of the code snippet in the post at https://www.experts-exchange.com/questions/27993208/What-does-this-error-mean.html?anchorAnswerId=38770066#a38770066.  This shows that the $control variable is set in the HTTP request - either POST or GET.

This latest block of code looks like an ancient relic of the PHP4 days.  It really needs to be refactored.  The problem might be in the code or the data -- and without any error checking or data validation, you can't know.  The queries could be failing, or even running but finding no data, and there is no useful error-data visualization.

This is a "start-over" project, not a question with any good answer.  If you want to continue to work on this thing, start by using var_dump() to print out the contents of all of the objects and begin your learning adventure there.
I should have said this before, but if you are fixing (debug) code (not your code), For me I usually work in reverse, I take the old code and use comment
/*
$oldCode = getV($old);
if ($oldCode == 0) echo 'oldCode is Zero';
moreOld(44);
*/

to remove as much as possible of the old PHP to see what the HTML output will be, or errors.
The un-comment One section of php code to see what HTML output has been added (or changed) and errors. As you have many sections in this php code, this may be somewhat limited.
Because of this line -
<?php include "login_script.php"; ?>
I would guess that this is the FIRST page presented to a browser so the user can "Log-In"
if this is First page then there are NO SESSION values preset ($__session empty) ? ?
so things like -
$_SESSION['color_01']  may not have a value (unset) ? ?
no where is $_SESSION['color_01'] set to value in this PHP (did a search).

Looked at the   class SystemClient{   code, no where is there a SystemClient Object created, as -
$_SESSION["SystemClient"] = new SystemClient($user);

so if that object is initialized, it's not in that code.
Although there is a Table colunm named 'control, in access, I do not see any direct application of that (object value to $control variable) in the page code, the only place where control is set is -
$control="main.null.null";  (I am assuming that there are NO POST or GET for this page).


Sorry for you man, but there does not seem to be much hope of unwinding and fixing this, except to start with ZERO php code and code it from the start,
you don't even sound as if you are sure of what HTML display this page is suppose to have, ,  to be correct and fixed?
I'm wondering if I'm even connecting to the database. I've tried several "print" configurations and I'm not able to get anything.

On the index page, on line 98 you have this:

	<?php
					$name=$_SESSION['SystemClient']->GetName();
					if (strpos($_SERVER['SERVER_NAME'],"localhost") && trim(strtoupper($name))=="MASTERa  CONTROL"){
						echo "width=\"1\"";
						echo $name;
					}?> 

Open in new window


I do an "echo $name;" and I get nothing.

How can I determine if I'm making a legitimate connection to the database?

class SystemClient{
 	
	var $id, $name,$username,$email,$privileges,$group_type,$group_id;
	
		
 	function SystemClient ($user_id){
		if (strpos($_SERVER['SERVER_NAME'],"snowdogweb")){
		$user="seth";
		$pass="newguy";
		$datasource="heavy_duty";
		$server="localhost";
		}else{
		$user = "hdldba";
		$pass="";
		$datasource = "hdlighting_";
		$server="localhost";
		}
		
			
		$dbase_user = mysql_connect($server,$user,$pass);
		mysql_select_db($datasource, $dbase_user);
		
		$query_results = mysql_query("SELECT users.id,users.first_name,users.last_name,users.m_inital,users.email,users.username,groups.type,users.group_id from users LEFT JOIN groups ON users.group_id = groups.id WHERE users.id='$user_id'", $dbase_user);
		print_r($query_results);
		$data = mysql_fetch_array($query_results);
		//print_r($data);
		$this->id = $data["id"];
		$this->name = $data["first_name"] ." ". $data["m_inital"]  ." ". $data["last_name"];
		$this->username = $data["username"];
		$this->email = $data["email"];
		$this->group_type = $data["type"];
		$this->group_id=$data["group_id"];
	}

Open in new window

You ask questions about this SystemClient  Class, , as I said I do not see any code that uses that class to create an object as -
$_SESSION["SystemClient"] = new SystemClient($user);

So if in the  login_script.php or other php file include, where it creates this object, you might do better there for research in how this code works for the SystemClient  Class.

if you modify the SystemClient  Class code as -

$dbase_user = mysql_connect($server,$user,$pass);
if ($dbase_user) echo "SC initialization has a Connect"; else echo "ERROR : SC initialization has NO Connect";

but that may cause problems if the HTML output is before the session_start();

you really should place Debug code outside of IF tests

 if (is_object($_SESSION["SystemClient"])) echo "session has Object<br>"; else echo "ERROR : session has NO object<br>";
$name=$_SESSION['SystemClient']->GetName();
echo 'GetName() return= ',$name;

lookin at this I would start with somethin like this -

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Testing CMS</title>
</head>
<body>
<?php
include('systemclient.inc');
$sc = new SystemClient('does not matter');
$n = $sc->GetName();
echo 'GetName() return= ',$n,'<br />';
// do other test code for SystemClient here. maybe
echo '<pre>';
var_dump($sc->privileges);
echo '</pre>';
?>
<div>
<?php include 'main.php'; ?>
</div>
</body></html>

Open in new window


just to start some where with the first basic,
THis is some kind of CMS according to the title , so that CMS code may make this more of a challenge? this also has some Includes for a gallery and PDF , but I see nothing in the code about a gallery (images) and PDF, so why are they included??
ASKER CERTIFIED SOLUTION
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
The error turned out to be two columns that were incorrectly named.

I had imported the database from saw raw mysql files and some things got misspelled in the process.

And now we're running just fine...
Great!  Thanks for the points and good luck with your project, ~Ray