Error handling in PHP with Error logger working

Error handling in PHP with Error logger working

Logging errors in php is easy, cool and helpful when it comes to test the site for errors as I explained it in my earlier post Logging errors to file in PHP but only till the site is at developer’s end and you working on it. Once you take the site live its just not good anymore. Once you take it on the production end, you cannot show the errors to the user or in case if its even 100% error free you will find that 1 in a 100 person who likes to messup with the sites. Being a programmer you gotta be ready for it. This is the place where comes error handling into play.

Here is a simple script. Just place these two functions in a file and include it at the right place. Probably including from the point onward from where you expect and error would be just perfect.

// Define a custom error handler
function userErrorHandler($errno, $errstr, $errfile = '', $errline = 0, $errcontext = array()) {
	// Getting error type
	$errorType = array (
			E_ERROR				=> 'ERROR',
			E_NOTICE			=> 'NOTICE',
	if (array_key_exists($errno, $errorType)) {
		$err = $errorType[$errno];
	} else {
	// Getting the error log file from php.ini
	$file 		= ini_get('error_log');
	// Creating the error log script, same as normal logger would do
	$error_string 	= "[" . date("d-M-Y H:i:s", $_SERVER['REQUEST_TIME']) . '] PHP ' . $err . '::' . $errstr . " in " . $_SERVER['SCRIPT_FILENAME'] . " on line " . $errline . "\r\n";
	// Logging error to a certain file
	error_log($error_string, 3, $file);
	// Check if the error code is not included in error_reporting
	if (!(error_reporting() & $errno)) {
	// Restore default handlers to prevent errors in errors
	if (function_exists('restore_exception_handler')) {
	// Load error page

// Define a custom handler for uncaught exceptions
if (function_exists('set_exception_handler')) {
	function userExceptionHandler($exception) {
		// Restore default handlers to prevent errors in errors
		if (function_exists('restore_exception_handler')) {
		// Load error page

Note: Once you start error handling in real time error logging stops. To overcome this a short code is included inside the function that uses PHP’s error_log function to log errors. So if you have a specifc error log file, just point it towards that.

Good luck!!!

Increase PHP Script Execution Time Limit

Every once in a while I need to process a HUGE file. Though PHP probably isn’t the most efficient way of processing the file, I’ll usually use PHP because it makes coding the processing script much faster. To prevent the script from timing out, I need to increase the execution time of the specific processing script. Here’s how I do it.

ini_set('max_execution_time', 60); //60 seconds = 1 minutes

Place this at the top of your PHP script and let your script loose!

MYSQL next increment value or last inserted value

Getting the next auto increment value and last inserted value from a mysql table are both two different things and should not be confused with one another. Next increment value should be the next id to be inserted and last inserted value is the last id inserted. Sometimes I see programmers using former code to get the latter task done and vice-versa. That might do it in some cases, but in the long run will surely fail.

Correct way of getting the value of next to be inserted id, is:

$query_autoinc 		= "SHOW TABLE STATUS LIKE 'table'";
$exec_autoinc 		= mysql_query($query_autoinc);
$row_autoinc 		= mysql_fetch_assoc($exec_autoinc);
$next_insert_id 	= $row_autoinc['Auto_increment'];

Continue reading “MYSQL next increment value or last inserted value”

N-level menu using recursion

Recently I needed to come up with a code that would help me create a n-level menu and breadcrumb for that. So for that I wrote this code which works with great ease and provides huge flexibility. Also I kept it really low so that it is easy to embedded it in any kind of design. This is just the half part of my code which prints the menu.


$table	= 'menu';
function showMenu($ulclass, $liclass, $father) {
	$showmenu_query = "select * from $table where menu_father='" . $father . "' order by menu_order";
	$exec_showmenu = mysql_query($showmenu_query);

	if ( !$exec_showmenu) {
		echo "Query failed. Check your query.

Error Returned: " . mysql_error(); return false; } $toAppend = '
    '; while($row_showmenu=mysql_fetch_assoc($exec_showmenu)) { $toAppend.= '
  • '; $toAppend.= '' . stripslashes($row_showmenu['menu_name']); $toAppend.= ''; $submenu_query="select * from $table where menu_father='" . $row_showmenu['menu_id'] . "'"; $exec_submenu = mysql_query($submenu_query); if( mysql_num_rows($exec_submenu)>0 ) { $toAppend.= $showMenu($ulclass, $liclass, $row_showmenu['menu_id']); } $toAppend.= '
  • '; } $toAppend.= '
'; return $toAppend; }

Continue reading “N-level menu using recursion”

Redirecting site or pages to a new domain or url aka 301 redirections

I learnt about 301 redirection when I moved my wordpress site from earlier domain to this new domain.

Htaccess redirect is better than the meta refresh or redirect tag because there is no delay as the browser reads the .htaccess file first. Here is how it works.

Go to your site’s root folder, download the .htaccess file to your local computer and edit it with a plain-text editor (ie. Notepad). If you are using FTP Client software and you don’t see any .htaccess file on your server, double check your setting and make sure you have turn on invisible / system files. Continue reading “Redirecting site or pages to a new domain or url aka 301 redirections”