display_errors is not working - php

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();
}

Related

PHP if(!#include_once()) doesn't work

This was working and then it stopped. That was in version 7.2.6 the latest.
if(!#include_once('config.php')) {
echo 'failed';
}
So we've downgraded to 7.1.9 but it doesn't work there as well.
There is no error thrown, nothing. Just a blank screen. It's as if it's not even there...
If I echo something before that, it works. If I echo something after this, nothing happens.
Why is this happening?
what, exactly, do you think # does? it suppresses errors. so when you say There is no error thrown, nothing, this is totally expected, because if there is any errors, # probably suppress them. remove the #, and if you can't trust your php.ini settings, do an ini_set call too, also set in some debug prints, eg
ini_set("error_reporting","-1");
echo "before include.";
if(!include_once('config.php')) {
echo 'failed';
}
echo "after include";
then check your error logs after it executed.
Maybe you could try the advice around "example #4" on this page and see if that makes a difference?
http://php.net/manual/en/function.include.php
E.g.
if ((#include_once 'config.php') === FALSE) {
echo "failed";
}
By the way: as other's have said, # suppresses PHP warnings/errors for the line of code where it's applied.
My interpretation of your question is that you have used # deliberately to suppress the engine's warning and echo your own message instead. If you removed the # and the include failed, I'd expect to see the warning message output if errors are set to show, AND the word 'failed' should still be echoed afterwards.
This is how I solved it.
if (!(#include 'config.php')) { die(json_encode(array('error'=>true))); }
I need the error to be suppressed because I wan't to handle the error without breaking my script—in a way that the other end know's it's an error and will inform the front-end user.

Notices in PHP. How to did not show them?

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.

Hide copy warnings in PHP

I know it's not a good practice to hide the warnings using #copy, but what other alternatives are there?
Is there any way you can make sure copy would work or not ?
Use is_readable() and is_writable() to check the status of the source and target before attempting the copy().
Really, you should not be displaying errors to the browser. Turn off display_errors in php.ini.
Then you can test if it succeeded by its boolean return value, without needing to worry about the warnings on screen.
if (!copy('srcfile', 'destfile')) {
// something failed.
}
If you use '#' before a function you'll not the warning or the notice returned but you'll keep the result (boolean, string...).
Try this :
if (!#copy('srcfile', 'destfile')) {
// something failed.
}

HTTP Error 500 when running php scripts

I am running a PHP page and as soon as I introduce calls like this: $_GET('') then everything goes wrong and I get an error 500.
This code goes not work:
echo $_GET('username');
echo $_GET('password');
?>
This code does:
<?php
phpinfo();
?>
The above code has syntax errors - you need to use square brackets.
The web server's error logs will show you those errors if you have access to them.
Use this:
echo "Username: ".$_GET['username']."<br />Password: ".$_GET['password'];
Since $_GET is a array and not a function, you need to use [square brackets] instead of (normal brackets) to retrieve the data out of a array.
To figure out what the problem is you need to turn on php error reporting. You do this by running this the first thing you do in your php-file:
ini_set('display_errors',1);
error_reporting(E_ALL);
E_ALL means the interpreter will show you errors, warnings and notices. After that, everything will be pretty obvious since php will tell you what went wrong.

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.

Categories