Jump to content

[SOLVED] Help with custom php error handling


jammesz

Recommended Posts

I have written my own error handling function in php.

function error($type, $error, $file, $line){
die('<meta http-equiv="refresh" content="0;url=error.php?t='.rawurlencode(base64_encode($type)).'&e='.rawurlencode(base64_encode($error)).'&f='.rawurlencode(base64_encode($file)).'&l='.rawurlencode(base64_encode($line)).'">');
}

error_reporting(0);
set_error_handler("error", E_ALL & ~E_NOTICE);

mysql_connect($cfg['mysql_server'],$cfg['ysql_username'],$cfg['mysql_password']) or trigger_error("Can't connect to database server", E_USER_ERROR);
mysql_select_db($cfg['mysql_database']) or trigger_error("Can't connect to database", E_USER_ERROR);

(the example error is the missing "m" from the username variable passed to mysql_connect)

 

My error function picks up this error but i want it to pick up my own personal error msg for the error first ( ...or trigger_error("Can't connect to database", E_USER_ERROR); ) before the default php one.

 

Anyone know how to do this???

Link to comment
Share on other sites

If you're asking whether you can make your trigger_error() occur before php's "undefined index" error:

 

Short answer:

You can't

 

Long answer:

You can't. It gets to the line and sees you are referencing an undefined index. It throws an error to tell you and THEN it processes the function as something similar to this...

 

<?php
mysql_connect($cfg['mysql_server'], '', $cfg['mysql_password']);

 

Note the empty string for the username (because that's what PHP is passing with an unset index - an empty string). For all PHP knows, an empty string could be a valid username. After the function returns false, only then is the trigger_error() function called.

 

if that's not what you're asking, could you clarify a bit?

Link to comment
Share on other sites

well the error it throws before my custom one is actually the same error (eg. mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO)).

 

i just wanted to replace with mine but i think you answered my question anyway. BTW i can still filter out php's database error msg in the error() function but it's a real hassle to do it this way and i was trying to see if there was a more efficient way.

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.