Notices in PHP. How to did not show them? - php

I has function which deleting the file. It is my code:
echo error_reporting(); // got 32767 = E_ALL, yes?
ini_set('display_errors',0);
if(unlink($file) == false){
echo "Error";
}
And I am getting the following error:
string(274) "PROBLEM unlink(/path/to/file.mp4): Permission denied in
/path/to/script.php on line 1226
How can I log my errors on log file, but not on the screen?

You can use your own error handler with function set_error_handler()
You can write logging in the function passed to set_error_handler()
This function has also param called error_types where you can provide what kind of errors you want to handle. It's up to you if you display them or not.
I won't write about other options because they were already mentioned by others.

you can use error_log() and for suppress the error in frontend you can use error_reporting(0)

The reason why it doesn't work is because in PHP 5.2.4 and up the variable has changed from boolean to string. Set display_errors to 'stderr' instead.

→ Try this:
This will work at runtime:
ini_set( 'display_errors', '0' );
This will stop errors from being displayed but they will still be logged. This will stop both:
error_reporting(0); // Will stop both error displaying and reporting to screen

It was var_dump() on other script which catching errors and show it on screen.

Related

PHP error reporting destination: error log file vs. web page

In the following code, I get the 'AAA' on the web page output (only), while the 'BBB' goes to the error.log file (only).
I want both to go to the error.log file, so the user doesn't see potential errors while I do. How?
<?php
ini_set('display_errors','1');
ini_set('error_log','error.log');
error_reporting(-1); // all
trigger_error('AAA');
error_log('BBB');
?>
(Note: This is similar to http://stackoverflow.com/questions/9921643/how-to-make-provoke-an-php-error-in-php-error-log but not the same.)
Set ini_set('display_errors', 0);
OR
Create your own error handler function and add it before calling trigger_error function with set_error_handler function.
This function can be used for defining your own way of handling errors
during runtime, for example in applications in which you need to do
cleanup of data/files when a critical error happens, or when you need
to trigger an error under certain conditions (using trigger_error()).

Suppressing errors in PHP

I'm running a PHP script every night using a cron service. Everything it outputs will be printed to an log file for debug prepossess. The file I use will retrieve xml's from a different site using the function 'file_get_contents()'. But the function can return an error which I really don't want to see as I am already showing a custom error.
Quick example of my piece of code:
$buffer = #file_get_contents('http://xx:xx#xx/xml/xx?offset=2') or print('retry in 5 seconds');
if($buffer === false) {
sleep(5);
$buffer = #file_get_contents('http://xx:xx#xx/xml/xx?offset=2') or print('error notice');
}
The problem is the first one will trigger an error and print it'll retry in 5 seconds. How can I correctly suppress the thrown error?
I have an error handler, but I prefer not to catch this error separately.
Edited:
My solution wasn't to change the error_reporting, but to catch the error message. If it starts with 'file_get_contents()', no error will be thrown. This is not the best way, but will do the job for me.
You can try inserting this at the start:
error_reporting(0);
Then after the code with the error/warning:
error_reporting(E_ALL ^ E_WARNING);
Okay, never ever use the #-operator.
In PHP you have two options available: either use a custom error handler or use try/catch.
Since file_get_contents doesn't throw an exception, you can only use the first approach.
You can set an error handler like this: http://php.net/set-error-handler and then act correctly (log something or return a custom error code).
If you just want to turn of all errors use error_reporting(0) or if you just want to turn off a specific category use error_reporting(E_ALL ^ E_WARNING) (all but warnings) or specifcy them explicitely error_reporting(E_WARNING | E_NOTICE) (warnings and notices).
I prefer the first approach, since when you just disable it you have no idea of what's going on in your code.
Add # before command or use try catch.

display_errors is not working

I'm trying to make errors hidden but it seems I'm doing something wrong.
In my hosting configuration display_errors is set to off and I don't have .htaccess file. I tried to write follownng script
<?php
echo ord(ini_get("display_errors")) . " ";
die("error");
?>
And I'm getting following output:
0 error
So, display_errors is set to off, but die() function still shows error on the screen. How to avoid this?
By doing die("error"), you're are commanding the code that it should output the string "error" and stop the code.
You are seeing the "error" message does not mean that there is an error, it is just another string.
die() is a function (commonly used to handle errors), but it is not deactivated when you set display errors off. It will still work and to whatever it is meant to do.
How to avoid this?
Do not use die() to handle errors.
use trigger_error() instead, which will follow the behavior you expected
There is a option to display error to stderr instead of stdout so you won't see them on you webpage, but only in the logs. (Manual)
I found error_log() function. Now I can use
function quit($error)
{
echo "Request failed";
error_log($error);
die();
}

What can I do to make my PHP web application fail in a more noisy way?

What can I do to make my PHP web application fail in a more noisy way?
I am using an MVC pattern and often when classes fail to load or failures they do so without error.
<?php
error_reporting( E_ALL );
ini_set( 'display_errors', '1' );
ini_set( 'log_errors', '1' );
function error_handler($errno, $errstr, $errfile, $errline) {
system('/usr/bin/mplayer /home/user/music/Moras_Modern_Rhythmists/Mr._Ghost_Goes_to_Town.mp3', $retval);
return true;
}
set_error_handler( "error_handler" );
?>
Depending on what your error reporting level is at, you could try raising it via .htaccess.
php_value display_errors 1
php_value error_reporting 2147483647
Use the require_once method to load your files instead of include.
I think that's what you're asking, right?
If you're doing testing, check whether your php.ini settings has display_errors property turned on.
<? ini_set("Melodramatic", "true"); ?>
The easy answer: die('A fatal error occurred')
In a PHP application I wrote, I came up with a convention of using a variable or class member named $err_msg which is initially set to null . When an error happens, set it to a human readable string. When it's time to check errors, check $err_msg and put it on display for the end-user. If it's an AJAX call, echo $err_msg on failure, echo 'OK' on success.
In my case, I wrote a simple jQuery-based status box that can display busy indicators and errors. When an AJAX call returns an error message, I make the status box fade in with a red background and display the error message. It's quite nice and uniform.

How to turn off mysql errors from being displayed to screen in CodeIgniter

Even though error_reporting is set to 0, database errors are still being printed to screen. Is there a setting somewhere I can change to disable database error reporting? This is for CodeIgniter v1.6.x
EDIT: Re: Fixing errors - Um, yes. I want to fix the errors. I get error notices from my error log, not from what my visitors see printed to their screen. That helps no one, and hurts my system's security.
EDIT 2: Setting error_reporting to 0 does not affect CodeIgniter's built-in error logging class from writing to the error log.
Found the answer:
In config/database.php:
// ['db_debug'] TRUE/FALSE - Whether database errors should be displayed.
so:
$db['default']['db_debug'] = FALSE;
... should disable.
In addition to Ian's answer, to temporarily disable the error messages:
$db_debug = $this->db->db_debug;
$this->db->db_debug = false;
// Do your sketchy stuff here
$this->db->db_debug = $db_debug;
You don't want to change
error_reporting to 0, because that will
also suppress errors from being
logged.
instead you should change
display_errors to 0
This doesn't explain why you are
getting errors displayed though,
assuming error_reporting is actually
0. Maybe the framework handles these errors
You can edit the /errors/db_error.php file under the application directory - that is the template included for DB errors.
However, you should really just fix the errors.

Categories