Jump to content

error_reporting custom message?


amb99

Recommended Posts

Hi,

 

My current error reporting is set to:

error_reporting  (E_ERROR | E_WARNING | E_PARSE);

 

Is there a way to use a default html page whenever any errors are encountered? (Instead of displaying the actual message of X error on X line for X script, this I would restrict to myself only, and for everyone else preferably would be only able to see the default page.)

 

Thank you for any help that you can provide.

Link to comment
https://forums.phpfreaks.com/topic/36703-error_reporting-custom-message/
Share on other sites

You could do something like this:

<?php
function custom_error_handler($errno, $errstr, $errfile, $errline, $errcontext)
{
echo <<<EOF
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>Application error</title>
</head>
<body>

<h1>Application error</h1>

<p>We're sorry, but we encountered an error in the script and are therefore unable to continue. We apologize for an inconvenience this may have caused.</p>

</body>
</html>
EOF;
die();
}
set_error_handler("custom_error_handler");
?>

 

With that you are also able to make some logging functions.

I've added that function and am using the code below, which is included in every script.

 

if ($user_ip == "x.x.x.x") {
error_reporting(E_ERROR | E_WARNING | E_PARSE);
}
else {
set_error_handler("custom_error_handler");
}

 

The problem is that the set_error_handler calls that function regardless if whether or not there is an error. Whereas, error_reporting only displays when an error has actually occured. So now I'm just trying to figure out how to write it so that set_error_handler is only called when an error actually occurs.

 

I've tried adding braces within the custom_error_handler function you suggested:

if (error_reporting()) { }

 

but that did not work. It still echo's even when there is no error occuring.

 

So I tried:

if (error_reporting(E_ERROR | E_WARNING | E_PARSE)) { }

 

Still no luck. I took a peek at error_reporting(E_ALL); and I do see some minor variable problems which might be the reason why when doing set_error_handler it immediately echos? Though I'm unsure. Is there a sure way to only display that default page when and only when an error has actually occured, just like how error_reporting(E_ERROR | E_WARNING | E_PARSE) works?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.