stop E_NOTICE

A

Anonymous

Guest
I use this to stop php from reporting notice errors

Code:
error_reporting(E_ALL ^ E_NOTICE);

I get a couple thousand in my error reporting script for a undefined index in a array. That should take care of the notices in my error report right? unfortionatly it does not. I on the top of my other scripts too.



Code:
<?php
error_reporting(E_ALL ^ E_NOTICE);
class ErrorHandler
{
	/** Array of errors */
	var $c_errors;
	
	/**
	 * Constructor - Registers error handling and shutdown functions with PHP
	 */
	function ErrorHandler ()
	{
		$this->c_errors = array();
		
		// Call shutdown function
		register_shutdown_function(array(&$this, 'onShutdown'));	
		set_error_handler(array(&$this, 'onError'))	;
	}
	
	/**
	 * Handle shutdown event
	 */
	function onShutdown ()
	{
		// Check any errors have been registered
		if ( ! $this->hasErrors() )
		{
			return;
		}
		
		// Dont generate errors if it's the DAMN search engine re-indexing everything
		if ( substr($_SERVER['HTTP_USER_AGENT'], 0, 11) == 'MnoGoSearch')
		{
			return;	
		}
		
		// Display message to user - this will display at the bottom of the page
		print('<hr />');
		print('<b>Sorry, errors have occured while processing you request</b>');
		print('<p>An administrator has been notified, and the problem should be fixed shortly');
				

		// Set the 'From' address, some SMTP servers only allow mail to be sent from specific domains
		$l_headers = 'From: ';
		$l_message = "";
	
		// Itterate through errors and add them to the list
		$i = 0;
		foreach  ($this->c_errors as $l_error)
		{
			$i++;
			
			$l_message .= "-- Error $i ---------------------------------------------------\n";
			$l_message .= "    Type: " . $this->getErrorDescription($l_error['type']) . "\n";
			$l_message .= "    Time: " . $l_error['time'] . "\n";
			$l_message .= "    Msg: " . $l_error['message'] . "\n";
			$l_message .= "    File: " . $l_error['file'] . ":" . $l_error['line'] . "\n";
			$l_message .= "    Ctx: " . $l_error['context'] . $l_error['context'] . "\n";
		}
		
		// Add debug information for Server
		$l_message .= "\n\n-- _SERVER: ---------------------------------------------------\n";
		$l_message .= print_r($_SERVER, true);
		
		// Add client 'GET' debug information
		$l_message .= "\n\n-- _GET: ---------------------------------------------------\n";
		$l_message .= print_r($_GET, true);
		
		// Add client 'POST' debug information
		$l_message .= "\n\n-- _POST: ---------------------------------------------------\n";
		$l_message .= print_r($_POST, true);
		
		// Add client 'SESSION' debug information
		$l_message .= "\n\n-- _SESSION: ---------------------------------------------------\n";
		$l_message = print_r($_SESSION, true);

		// And finally e-mail the report to the administrator
		mail ('', $_SERVER['SERVER_NAME'] . ' error report', $l_message, $l_headers);
		echo $l_message;
		return;
	}
	
	/**
	 * Get a description of the error given it's code
	 * @param $a_code Error number that represents it (e.g. E_USER_ERROR)
	 * @return Text string describing the error
	 */
	function getErrorDescription ($a_code)
	{
		switch ($a_code)
		{
		case E_COMPILE_ERROR:
			return "Compile Error";
			
		case E_COMPILE_WARNING:
			return "Compile Warning";
			
		case E_CORE_ERROR:
			return "Core Error";
			
		case E_CORE_WARNING:
			return "Core Warning";
			
		case E_ERROR:
			return "Error";
			
		case E_WARNING:
			return "Warning";
			
		case E_NOTICE:
			return "Notice";
			
		case E_PARSE:
			return "Parse";
			
		case E_USER_ERROR:
			return "Error (User Generated)";
			
		case E_USER_WARNING:
			return "Warning (User Generated)";
			
		case E_USER_NOTICE:
			return "Notice (User Generated)";
		
		// Unknown!
		default:
			return "Unknown ($a_code)";
		}	
	}
	
	/**
	 * Check if there have been any errors registered
	 * @return true if there have been, false if not
	 */
	function hasErrors ()
	{
		if ( count($this->c_errors) )
		{
			return true;	
		}	
		
		return false;
	}
	
	/**
	 * Handle error event
	 * @param $errno Error type (e.g. E_USER_ERROR)
	 * @param $errstr Message that describes the error
	 * @param $errfile File that the error occured in
	 * @param $errline Line of that file that the error occured on
	 * @param $errcontext Context that the error happened within
	 */
	function onError ($errno, $errstr, $errfile, $errline, $errcontext)
	{
		// Create error struct
		$l_error = array();
		$l_error['type'] = $errno;
		$l_error['message'] = $errstr;
		$l_error['file'] = $errfile;
		$l_error['line'] = $errline;
		$l_error['context'] = $errcontext;
		$l_error['time'] = strftime('%d/%m/%y - %T');
		
		// And just add it to the end
		array_push($this->c_errors, $l_error);
		
	}	
}

// Register the error handler


// EOF
?>
 
I used to hide my scripts' E_NOTICEs, but... i've learned something... with Swirlee (Muah)
We better have a clean code and get rid of EVEN the E_NOTICEs in our code.
Today i can't see an E_NOTICE. I do clean it up at the same time!

error_reporting(E_ALL);
ini_set('display_errors', 1);


:D
 
true, its best for development enviroment, but when you are in production enviroment you should hide all your error message and E_NOTICE should simply be ignored in a live application..
 
I agree 100%
ini_set('display_errors', 1); Until i make sure my code is all very well done (with less errors as possible).
 
Yeh, I like it :D
I just change from server and don't have this kind of things online yet :)
 
one thing that pissed me off the most with E_NOTICE on : in my platform it gave me a notice about$_SESSION['SID'] : SID not being a defined index, and then it was supposed to send a header("Location") and it, ofcourse failed... i hate that..
 
Really don't know what can cause that... it never happened with me!

I also have a strange problem and can't figure it out, maybe you can help me.
I change my host. It was using PHP installed as an Apache module (And using Apache 2.48 ). This new server has PHP installed as CGI (And using Apache 1.33). My problem now is that i get that amazing error we all know when calling my PHP headers' function. Also i can't set a cookie with php. I'm sure my code is done the way nothing, but other headers information, is sent before a header.

Well, i bet it's just something in the php.ini, but...
Do you know what can cause this ?

Just a note: A great reference of what happens when using PHP as CGI.
 
Wat happends when using PHP as a CGI: Everything gets f***ed up... excuse me for my language :)
thats weird problem... whats your script?
 
Thanks for asking :D
You made me search for white spaces after all my included files' code... that was the problem!
I just don't get how it was working in the other server. There must be some .ini setting that ignore that white spaces or something, insn't it ? I don't know :p

Thanks
 
But they do :p I've already made it once.
And other thing is that i get a 500 Internal Server Error in "everything" and don't even know why. Grrrr! :-x
I must ready a bit more about using PHP as CGI :)
 
Back
Top