php set_error_handler not working properly - php

I defined handler function in functions.php:
function errorHandler($errno, $errstr, $errfile, $errline) {
echo "An error: $errstr";
return true; // Preventing standard handler
}
Then I used it in set_error_handler:
require_once('functions.php');
set_error_handler("errorHandler");
But when I trigger notice, I get standard PHP message: Notice: Undefined variable: thisVariableDoesntExist in..., set_error_handler doesn't handle E_ERROR, but here I have notice, so I can't get it why this doesn't work.

Related

myErrorHandler getting error

I am getting following errors messages:
Notice: Use of undefined constant myErrorHandler - assumed 'myErrorHandler' in C:\xampp\htdocs\Webpage\Security\functions.php on line 78
and
Warning: set_error_handler() expects the argument (myErrorHandler) to be a valid callback in C:\xampp\htdocs\Webpage\Security\functions.php on line 78
I cant figure out what I need to do to correct the error. I want the function myErrorHandler to handle all the error's on the webpage.
function DB_Connect()
{
static $conn;
if (!$conn) {
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect(DEF_Server,DEF_User,DEF_Password,DEF_Database);
}
return $conn;
}
set_error_handler(myErrorHandler);
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
error_log("$errstr in $errfile:$errline");
header('HTTP/1.1 500 Internal Server Error', TRUE, 500);
readfile($NAV_DB_Error500);
exit;
}
I have fixed following per "SebTM":
set_error_handler("myErrorHandler");
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
error_log("$errstr in $errfile:$errline");
header('HTTP/1.1 500 Internal Server Error', TRUE, 500);
readfile($NAV_DB_Error500);
exit;
}
I am now getting following error, it removed the Notice, but the Warning is still there:
Warning: set_error_handler() expects the argument (myErrorHandler) to be a valid callback in C:\xampp\htdocs\Webpage\Security\functions.php on line 78
You need to pass the name of the function (after declaration) to "set_error_handler" as string e.g.
set_error_handler("myErrorHandler");
An error-handler should also return "true" as boolean to avoid internal php error-handling!

Error Handling: set_error_handler() expects the argument (errorHandler) to be a valid callback

I've got a PHP form with several lines of code.
Now I want to log errors in the database.
I'm trying to do this by the following code:
<?php
set_error_handler("errorHandler");
//The following line produces an error for testing
echo $notexist;
function errorHandler($errno, $errstr, $errfile, $errline) {
echo "error detected";
}
?>
Unfortunately PHP throws an error and I can't find out how to fix it:
"set_error_handler() expects the argument (errorHandler) to be a valid callback"
I think I defined the callback, didn't I?
If the function is in a namespace you need to put the fully qualified name of the function (including the namespace).
For example:
<?php
namespace MyNamespace;
set_error_handler("MyNamespace\t_errorHandler");
echo $notexist;
function t_errorHandler($errno, $errstr, $errfile, $errline) {
echo "error detected";
}
?>

Custom error function won't call in php

I am trying to create a custom error function (following a tutorial). I have:
<?php
error_reporting(E_ERROR);
function handleError ($errno, $errstr,$error_file,$error_line){
echo "<b>Error:</b> [$errno] $errstr - $error_file:$error_line";
echo "<br />";
echo "Terminating PHP Script";
die();
}
//set the error handler here, override the default
set_error_handler("handleError");
//cause a crash
myfunction();
?>
However my script isn't calling the function. It just prints the default error message. Could someone give me a pointer to what I might be doing wrong here please? Is my error_reporting value wrong?
There is nothing wrong with your code. However, the capabilities of set_error_handler are limited.
From the PHP documentation:
The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING,
E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised
in the file where set_error_handler() is called.
If you indeed need to catch compilation errors as in your example, there is one workaround mentioned in the comments of the documentation above - the use of a shutdown function:
<?php
error_reporting(E_ERROR);
function handleError($errno, $errstr, $error_file, $error_line) {
echo "<b>Error:</b> [$errno] $errstr - $error_file:$error_line";
echo "<br />";
echo "Terminating PHP Script";
die();
}
function checkForError() {
$error = error_get_last();
if ($error["type"] == E_ERROR) {
handleError($error["type"], $error["message"], $error["file"], $error["line"]);
}
}
register_shutdown_function("checkForError");
// cause a crash
myfunction();
?>
Note that the default error handler will still be called, so this prints out:
Fatal error: Call to undefined function myfunction() in path\to\file.php on line 24
Error: [1] Call to undefined function myfunction() - path\to\file.php:24
Terminating PHP Script
You can get rid of the default message by disabling error reporting with error_reporting(0);.
If you want to handle errors from within your method execution (i.e. you have defined myfunction somewhere), your original example might already work, depending on your concrete case. Proof, e.g.:
<?php
error_reporting(E_ERROR);
function handleError($errno, $errstr, $error_file, $error_line) {
echo "<b>Error:</b> [$errno] $errstr - $error_file:$error_line";
echo "<br />";
echo "Terminating PHP Script";
die();
}
function myfunction() {
fopen("nonexistingfile", "r");
}
// set the error handler here, override the default
set_error_handler("handleError");
// cause a crash
myfunction();
?>
This uses the custom error handler as expected and prints out:
Error: [2] fopen(nonexistingfile): failed to open stream: No such file or directory - path\to\file.php:12
Terminating PHP Script
I got this code from w3schools, I think you might have a need to trigger it
<?php
// A user-defined error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline) {
echo "<b>Custom error:</b> [$errno] $errstr<br>";
echo " Error on line $errline in $errfile<br>";
}
// Set user-defined error handler function
set_error_handler("myErrorHandler");
$test=2;
// Trigger error
if ($test>1) {
trigger_error("A custom error has been triggered");
}
?>

Exit execution on calling disabled function [duplicate]

Normally php script continues to run after E_NOTICE, is there a way to elevate this to fatal error in context of a function, that is I need only to exit on notice only in my functions but not on core php functions, that is globally.
You could create a custom error handler to catch E_NOTICEs.
This is untested but should go into the right direction:
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if ($errno == E_USER_NOTICE)
die ("Fatal notice");
else
return false; // Leave everything else to PHP's error handling
}
then, set it as the new custom error handler using set_error_handler() when entering your function, and restore PHP's error handler when leaving it:
function some_function()
{
// Set your error handler
$old_error_handler = set_error_handler("myErrorHandler");
... do your stuff ....
// Restore old error handler
set_error_handler($old_error_handler);
}
You use a custom error handler using set_error_handler()
<?php
function myErrorHandler($errno, $errstr, $errfile, $errline) {
if ($errno == E_USER_NOTICE) {
die("Died on user notice!! Error: {$errstr} on {$errfile}:{$errline}");
}
return false; //Will trigger PHP's default handler if reaches this point.
}
set_error_handler('myErrorHandler');
trigger_error('This is a E_USER_NOTICE level error.');
echo "This will never be executed.";
?>
Working Example

how to make php exit on E_NOTICE?

Normally php script continues to run after E_NOTICE, is there a way to elevate this to fatal error in context of a function, that is I need only to exit on notice only in my functions but not on core php functions, that is globally.
You could create a custom error handler to catch E_NOTICEs.
This is untested but should go into the right direction:
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if ($errno == E_USER_NOTICE)
die ("Fatal notice");
else
return false; // Leave everything else to PHP's error handling
}
then, set it as the new custom error handler using set_error_handler() when entering your function, and restore PHP's error handler when leaving it:
function some_function()
{
// Set your error handler
$old_error_handler = set_error_handler("myErrorHandler");
... do your stuff ....
// Restore old error handler
set_error_handler($old_error_handler);
}
You use a custom error handler using set_error_handler()
<?php
function myErrorHandler($errno, $errstr, $errfile, $errline) {
if ($errno == E_USER_NOTICE) {
die("Died on user notice!! Error: {$errstr} on {$errfile}:{$errline}");
}
return false; //Will trigger PHP's default handler if reaches this point.
}
set_error_handler('myErrorHandler');
trigger_error('This is a E_USER_NOTICE level error.');
echo "This will never be executed.";
?>
Working Example

Categories