myErrorHandler getting error - php

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!

Related

php set_error_handler not working properly

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.

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";
}
?>

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 get file_get_contents() warning instead of the PHP error?

file_get_contents('https://invalid-certificate.com');
Yields the following PHP warning and error:
PHP warning: Peer certificate CN='*.invalid-certificate.net' did not match expected CN='invalid-certificate.com'
PHP error: file_get_contents(https://invalid-certificate.com): failed to open stream: operation failed
I want to use exceptions instead of the PHP warning, so:
$response = #file_get_contents('https://invalid-certificate.com');
if ($response === false) {
$error = error_get_last();
throw new \Exception($error['message']);
}
But now the exception message is:
file_get_contents(https://invalid-certificate.com): failed to open stream: operation failed
That's normal, error_get_last() returns the last error…
How can I get the warning, which contains much valuable information regarding the failure?
You can make good use of set_error_handler and convert those errors into exceptions and use exceptions properly
<?php
set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
try {
$response = file_get_contents('https://invalid-certificate.com');
} catch (ErrorException $e) {
var_dump($e); // ofcourse you can just grab the desired info here
}
?>
A much simpler version would be
<?php
set_error_handler(function($errno, $errstr) {
var_dump($errstr);
});
$response = file_get_contents('https://invalid-certificate.com');
?>
Fiddle

Automatically record/log request URI as part of error in PHP?

Is there some way to get the request URI automatically added to error_log() output? Errors currently look like:
[03-Dec-2012 13:56:22] PHP Fatal error: Call to a member function getStories()
on a non-object in /usr/share/php/MyProject/Model/Index.php on line 148
Is there a way to get the URL in there?
Hope this helps:
function debugErrorHandler($errno, $errstr, $errfile, $errline)
{
if(error_reporting()!==0)
{
switch($errno)
{
default:
error_log("PHP Warning Debug: Server Request URI: " .
print_r($_SERVER["REQUEST_URI"], true));
break;
}
return false; // false -> Execute PHP internal error handler
}
}

Categories