Jump to content

problem using recursion, trying to get info from nested arrays


d_rock

Recommended Posts

Hi

 

I'm trying to customize my own error handler and am running into a problem.  Basically I wrote a function that is called to format $e_vars.  The first call to this function works fine and all of the elements of $e_vars are printed. However, alot of them are Arrays so i want to recursively call my function on these Arrays to get their values as well. 

 

When i try to run my program, Firefox doesn't seem to read the php file properly, instead it asks me if i want to open it in a text viewer, as if it is downloading the file. 

 

1. Why is FF trying to download the file instead of process it like a normal php file (by FF i mean my localhost Apache on my linux laptop)?

2. Can somebody offer a suggestion for my buggy code?  I've marked where the problem is. If you comment the line out, FF loads the page normally, but when uncommented, the problem occurs. Thanks for any help.

 

Config file:

// Create the error handler.
function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) {

    //global $debug, $contact_email;
    $debug = true;
$contact_email = "me";

    // Build the error message.
$premsg = ("<div width='75%' style='margin:auto;padding:10px;'><table style='font-size:small;' border='1' cellspacing='0' cellpadding='5' width='75%'>" .
			"<tr style='background-color:#005511;color:white;'>" . 
			"<td style='font-size:15px;font-weight:bold;' align='center' colspan='2'>A PHP Error has Occured</td></tr>" .
			"<tr><td style='color:white;' align='left' bgcolor='#00AC22' colspan='2'>General</td></tr>");
$postmsg = ("<tr style='background-color:#005511;color:white;'><td style='font-size:15px;font-weight:bold;' align='center' colspan='2'>darren@myavalon.ca</td></tr></table></div>");
$message = "<tr><td>Script</td><td>" . $e_file . "</td></tr>";
$message .= "<tr><td>Line Number</td><td>" . $e_line . "</td></tr>";
$message .= "<tr><td>Description</td><td>" . $e_message . "</td></tr>";
$message .= "<tr><td>Date</td><td>" . date('l dS \of F Y h:i:s A') . "</td></tr>";
$message .= ("<tr><td style='color:white;' align='left' bgcolor='#00AC22' colspan='2'>Variables</td></tr>");
$message .= digArray($e_vars);
echo "<p style='background-color:#FF0000;'>the type of e_vars is: " . gettype($e_vars). "</p>";
    
    if ($debug) { // Show the error.
    
        echo $premsg . $message . $postmsg;
        
    } else { 
    
        // Log the error:
        error_log ($message, 1, $contact_email); // Send email.
        
        // Only print an error message if the error isn't a notice or strict.
        if ( ($e_number != E_NOTICE) && ($e_number < 2048)) {
            echo '<p class="error">A system error occurred. We apologize for the inconvenience.</p>';
        }
        
    } // End of $debug IF.

} // End of my_error_handler() definition.

// helper function for the error handler to dig into the variable arrays and print them out
function digArray($array){
$hh = ("<tr><td style='color:black;' align='left' bgcolor='#cceecc' colspan='2'>");
$ht = ("</td></tr>");
$rh = ("<tr><td align='right'>");
$rt = $ht;
$ret;
foreach($array as $key=>$val){
	if (is_array($val)){
		$ret .= $hh . $key . " is " . $val . $ht;
		$ret .= digArray($val); // PROBLEM IS HERE!!
	}
	else {
		$ret .= $rh . $key . " </td><td> " . $val . $rt;
	}
}
return $ret;
}


// Use my error handler:
set_error_handler ('my_error_handler');

 

Test file that includes the config file (run this one to test)

include("./config.php");
echo $_POST['crash_dammit!!!'];

 

Link to comment
Share on other sites

downloading php file) I believe the only cause for this is your apache not being told what to do with a .php file. In your httpd.conf file there shoud be a line something like:

AddType php application/x-httpd-php .php

At least thats all my memory is coming up with at the moment.

 

recursing multi-dimensional array) in the function that reads the array you need to check if the element is an array ( is_array() ) if it is (and only if) you call the function again.

function parseArray($A){
   foreach($A as $E){
      if(is_array($E)){
         parseArray($E)
      }
   }
}

 

...thats what I have off the top of my head, hope it helps.

Link to comment
Share on other sites

hi mark, thanks for your help.

 

I have my config file working now. I added what you said about is_array(), also I had to deal with the  GLOBALS array differently, as there seemed to be some built in recursion there... very strange.

 

Anyways, my problem of FF wanting to download the .php file instead of display it is still happening.  Its weird, when i use the error handler with some projects it works fine, but when i include it in others, i get the download prompt instead of the page showing. I believe it is a setting in my apache conf. Perhaps too much recursion is going on, or too much memory being used? i ramped up my resource settings in myphp.ini file, but that hasn't worked.  i'll dig into the apache config files next.  If anybody has any ideas, I'd much appreciate them. Thanks again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.