Error log and display settings without ini_set()? - php

I'm trying to get error logging working so that errors don't show on my live website to people other than me (identified by my IP address), but it's not working because apparently ini_set() is disabled for my server due to "security reasons." Here's the code I was trying to use:
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', '/path/to/php-error.log');
I don't want to hard code the option to disable showing errors, because I do a check in my PHP pages to see if it's my IP address visiting the website and if so still show the errors.
Is there any way to log errors, dynamically changing whether they are displayed on the page or not, without using ini_set()?

You should take a look at PHP's in-built custom error handling functions, which you could build your verification into. This replaces PHP's error handler with a custom function.
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
$ip = $_SERVER['REMOTE_ADDR'];
if ( $ip != '127.0.0.1' ) return true; // Leave if not your IP
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
set_error_handler("myErrorHandler");
More info from the manual at: http://www.php.net/manual/en/function.set-error-handler.php

Related

XAMPP is able to modify headers after printing HTTP

I have XAMPP 5.6.3 on my development environment. I have a problem that my development environment is too tolerant. The problem is that if I run this on my development environment
echo "1";
header("Location: /homepage.php");
it will redirect to homepage without problem.
But if I do the same on my production environment, it would give the common error of (Cannot modify header information).
What I want is to receive the same fatal error on my development environment.
Thanks in advance!
You need to turn on error reporting to receive the error. You can do it from code or by modifying php.ini
From code
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Or find php.ini file in xampp/php directory and modify these line
display_errors=On
error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT
You can choose any other Error Level Constant
You can also check this answer How do I get PHP errors to display?
Edit: You can use set_error_handler function to modify default error behavior. Here is the documentation for this function https://www.php.net/manual/en/function.set-error-handler.php
Here is a example code to make warning fatal
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting, so let it fall
// through to the standard PHP error handler
return false;
}
switch ($errno) {
case E_WARNING:
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP ".PHP_VERSION." (".PHP_OS.")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
error_reporting(E_ALL);
set_error_handler("myErrorHandler");

What to do when error_get_last() is null?

I'm having difficulty debugging my PHP Application.
When I include my php file, no exception is thrown. I tried using set_error_handler, which does fire, but error_get_last() returns null.
set_error_handler(function() {
echo 'Here is your error!';
var_dump(error_get_last());
echo 'Sike!';
});
try {
include('viewer.php');
} catch (Exception $e) {
echo 'I really ought to tell you something went wrong here. But I won\'t';
}
restore_error_handler();
exit();
Output:
Here is your error!NULL Sike!
Is there some third way I can find out what actually went wrong?
from php docs try this
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting, so let it fall
// through to the standard PHP error handler
return false;
}
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
set_error_handler("myErrorHandler");

php custom error handler how to print error in production?

I have an interface calling some php function by ajax.
There is an uncatched error that I have only in production. Due I make estensive use of getJson data exchange , I needed to silent errors and warnings with a custom php error handler.
The problem is that anyway when code generates an error in production I wish to print it out to the user, to understand where the problem is. My code is :
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_ERROR:
case E_USER_ERROR:
$error= "<b>My ERROR</b> [$errno] $errstr<br />\n";
$error.= " Fatal error on line $errline in file $errfile";
$error.= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
$error.= "Aborting...<br />\n";
if(isAjaxCalling()){
$_SESSION['Errors']['Errors'][]=$error;
}
ini_set('display_errors',1);
ini_set('error_reporting','E_ALL');
echo $error;
exit(1);
break;
case E_WARNING:
case E_USER_WARNING:
....
}
You need to let know the ajax handler on the client side that the returned value is in fact an error message, best to do it with status header:
header("HTTP/1.0 500 Internal server error");
Then either globaly, or individualy per ajax request, set function that will handle the error. In jQuery, how to do it globaly: http://api.jquery.com/ajaxError/ or individualy set 'error' option of ajax request.
Seems it's now printed with this mod:
case E_ERROR:
case E_USER_ERROR:
$error= "<b>My ERROR</b> [$errno] $errstr<br />\n";
$error.= " Fatal error on line $errline in file $errfile";
$error.= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
$error.= "Aborting...<br />\n";
if(isAjaxCalling()){
$_SESSION['Errors']['Errors'][]=$error;
}
exit( $error);
break;

php global error array to log error

I am developing an website but when am escaping then inserting records into database, I am using try catch.
I want to create a global error array which I can add to any exception message if the try fails and catching an exception.
This array would later be used on my php page to show the error that occurred to the user.
How would I go about creating this array?
Thanks,
You can create a generic error handler like this one:
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
// set to the user defined error handler
set_error_handler("myErrorHandler");

PHP File Size Limit Message

We are using post_max_size and upload_max_filesize PHP variables to prevent users from uploading big files into our web app.
The thing is that the error message that PHP throws is ugly for the users and does not say much (especially for spanish users). So the users report this as a bug or think it is not working.
How can I change this page and show our own page (something more user friendly and in Spanish)?
Thanks a lot!
I think you're looking for set_error_handler().
So something along the lines of (from the manual):
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}
switch ($errno) {
case UPLOAD_ERR_INI_SIZE:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
$old_error_handler = set_error_handler("myErrorHandler");
catch the error and output a custom message.
try {
// your code
} catch (Exception $e) {
if($e->getCode() == some code){
$message = 'some message';
}else{
$message = 'some other message';
}
}
echo '<div>'.$message.'</div>'

Categories