SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ErrorHandling errors

    I am tackling error handling and am having a problem. I am trying to write my own custom error function and want to pass my variables into the function. Can anyone tell me what is wrong? I am trying to pass the $config array, but the config array does not get sent.

    Chris

    Code:
    <?
    // This is a custom error handler that will record any errors (except fatal errors) and
    // place them into a text file.  A variable will need to be set that will decide whether or
    // not to send an email to the webmaster and others.
    
    // Configuration
    // Send Email to who???
    $config[email][0] = "email goes here";
    
    // Who is the email from?
    $config[email][from] = "server goes here";
    
    // Path to write the error file to
    $config[error][path] = "./";
    
    // File name to write the errors to
    $config[error][filename] = "errors.log";
    
    // The Error Function
    function errorHandler($type, $msg, $file, $line, $config){
    	
    	// Need to read in some enviroment variables
    	global $HTTP_HOST, $HTTP_USER_AGENT, $REMOTE_ADDR, $REQUEST_URI;
    	
    	// Time to construct the error string
    	$errorString = "Date: " / date("d-m-Y H:i:s", mktime()) . "\n";
    	$errorString .= "Error type: $type\n";
    	$errorString .= "Error message: $msg\n";
    	$errorString .= "Script: $file($line)\n";
    	$errorString .= "Host: $HTTP_HOST\n";
    	$errorString .= "Client: $HTTP_USER_AGENT\n";
    	$errorString .= "Client IP: $REMOTE_ADDR\n";
    	$errorString .= "Request URI: $REQUEST_URI\n\n";
    	
    	// Time to write the error file
    	$file = $config[error][path] . $config[error][filename];
    	$fp = fopen($file, "a+"); // a+ - append to file
    	fwrite($fp, $errorString);
    	fclose($fp);
    	
    	//Time to send the email 
    	// We need to loop through the $config[email] array and configure the $to string.
    	
    	
    	$to = "$config[email][0]";
    	$subj = "An Error has been logged";
    	$msg = "This is an automated email.  An error has been detected and logged. Below is a copy of what information was logged.\n\n$errorString";
    	$hdrs = "From: $config[email][from]";
    	mail($to, $subj, $mssg, $hdrs);
    }
    
    
    error_reporting(E_ERROR | E_WARNING);
    
    // Define the error handling function
    set_error_handler("errorHandler");
    
    join(" ", $string);
    
    ?>

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Repost script using the PHP tags; not CODE.

  3. #3
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops. Try this.

    PHP Code:
    <?
    // This is a custom error handler that will record any errors (except fatal errors) and
    // place them into a text file.  A variable will need to be set that will decide whether or
    // not to send an email to the webmaster and others.

    // Configuration
    // Send Email to who???
    $config[email][0] = "email here";

    // Who is the email from?
    $config[email][from] = "server here";

    // Path to write the error file to
    $config[error][path] = "./";

    // File name to write the errors to
    $config[error][filename] = "errors.log";

    // The Error Function
    function errorHandler($type$msg$file$line){
        
        
    // Need to read in some enviroment variables
        
    global $HTTP_HOST$HTTP_USER_AGENT$REMOTE_ADDR$REQUEST_URI;
        
        
    // Time to construct the error string
        
    $errorString "Date: " date("d-m-Y H:i:s"mktime()) . "\n";
        
    $errorString .= "Error type: $type\n";
        
    $errorString .= "Error message: $msg\n";
        
    $errorString .= "Script: $file($line)\n";
        
    $errorString .= "Host: $HTTP_HOST\n";
        
    $errorString .= "Client: $HTTP_USER_AGENT\n";
        
    $errorString .= "Client IP: $REMOTE_ADDR\n";
        
    $errorString .= "Request URI: $REQUEST_URI\n\n";
        
        
    // Time to write the error file
        
    $file $config[error][path] . $config[error][filename];
        
    $fp fopen($file"a+"); // a+ - append to file
        
    fwrite($fp$errorString);
        
    fclose($fp);
        
        
    //Time to send the email 
        // We need to loop through the $config[email] array and configure the $to string.
        
        
        
    $to "$config[email][0]";
        
    $subj "An Error has been logged";
        
    $msg "This is an automated email.  An error has been detected and logged. Below is a copy of what information was logged.\n\n$errorString";
        
    $hdrs "From: $config[email][from]";
        
    mail($to$subj$mssg$hdrs);
    }

    error_reporting(E_ERROR E_WARNING);

    join(" "$string);

    ?>

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ummm....

    I think that your should have $config as a global variable ? Since you cannot directly pass it along to the error handler you've set up:
    PHP Code:
    # at the top of your error handler function

    global $config;
    .
    .
    # rest of script 
    I'm a bit puzzled as to why you have the current globals though ? Why not simply access them directly within the script from their actual variable name ?

    --EDIT--

    Also you may have a problem in the event that the variable $config is not set up prior to the error handler being executed; ie the data is not going to the array ?

    If there was a way of calling an external function from within the error handler to set the array; since the part of your script that creates the $config array wouldn't be executed prior to the error handler.

    Maybe use

    PHP Code:
    trigger_error($variables go hereE_WARNING); 
    Which calls your error handler; so I use this myself for example:

    PHP Code:
    # configuration
     
    @ include_once('templates/config/config.cfg');
     if(!
    defined('DB_SOURCE')) {
      
    trigger_error('unable to import required file: templates/config/config.cfg'E_USER_WARNING);
      return 
    false;
     } 
    If the file isn't included... You could put your variables within a delimited string and from error handler, explode this string and then put the variables directly to the email, bypassing $config ?

    Is that not a better option ?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •