I am trying to find how to force php server to throw error 500 when parsing php files fail. The reason is that I use php to build backend service. I can only do it in the .htaccess file.
At this stage my errors are entirely enabled, so when something goes wrong in php I get nice message where is the problem.
How can I disable displaying error and throwing just 500 when there is syntax error in php file ? Most of the cases I tried to find on web deal with opposite problem :D
is this what you need ?
function my_error_handler()
{
$last_error = error_get_last();
if ($last_error && $last_error['type']==E_ERROR)
{
header("HTTP/1.1 500 Internal Server Error");
echo '...';//html for 500 page
}
}
register_shutdown_function('my_error_handler');
Seems like you wanna use .htaccess to enable / disable your PHP errors to display.
In .htaccess you can set params of php.ini as below.
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
php_flag log_errors on
php_value error_log /home/path/public_html/domain/PHP_errors.log
Basically, .htaccess is good idea because it will reflect on your project level not the server level.
Related
I've been bashing my head trying to port some super old PHP script to PHP 7.0, which is proving impossible because I can't figure out what the errors are! If I implement some sort of syntax or parse error, the script happily shows those to me, but I cant get any errors that tell me my function is undefined when I try to call lkasjdfalkjshdfasdF(); (which obviously isn't set).
Things I've tried:
This is my .htaccess file in the same directory as my script
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
php_flag log_errors on
php_value error_log "/var/log/apache2/error.log"
I also have this at the top of my script
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
If I add echho 'test'; on one of my lines, it shows the following on the page, and in the error log
Parse error: syntax error, unexpected ''test'' (T_CONSTANT_ENCAPSED_STRING) in /domains/stupidphpscripts.com/public_html/lib/dumbfile.php on line 200
If I add echo asdfasdf(); on one of my lines, I get no output, and there's nothing in the error log.
I even wrote this silly function
$DebugPointCointer = 0;
function DebugPoint() {
global $DebugPointCointer;
echo "Debug point $DebugPointCointer on line " . debug_backtrace(1)[0]['line'] . "\n";
$DebugPointCointer++;
}
So I could make sure that the script was failing where I suspected, but in all honesty, that information didn't really help me.
What's left to try?
E_ALL is not enough.
use:
error_reporting(-1); // for all and the future (still un inplemented features) in develop enviroment!
Do you really run on the correct host?
May be a vhost that stores to a different error log?
Does the main scripts have any ats "#" -> #functionToCall() included which suspress anything ?
I need PHP errors not to be displayed but logged. I am using PHP 5.4 My current code to log errors in my php.ini is:
log_errors = 1
error_log = "/path-to-file/error_log.txt"
Which works however I am getting a 500 internal server error trying to turn error displaying off using display_errors. I have tried using the following, all returning 500 errors.
display_errors = 0
display_errors = "0"
display_errors = false
display_errors = "false"
display_errors = Off
display_errors = "Off"
According to the PHP documentation, as of PHP 5.4, it is a string. What am I suppose to set display_errors to to turn error displaying off?
a 500 error code means there is an invalid server configuration. This is most likely coming from Apache and not from php.
To get a clear understanding on what is giving you the error, look at your apache logs.
If you wish to also hide the 500 error,
you could open your httpd.conf file and add
ErrorDocument 500 " "
Then make sure to restart apache. Any time you make a config change to php.ini or httpd.conf you will need to restart apache for it to take into effect.
I noticed that when I issue a die statement in php, it's output doesn't appear on the error log (If i put an error_log statement right beside it it shows fine though).
I tried everything.. Here are the settings I did to make sure I see all my error logs:
php.ini:
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
error_log = /Applications/XAMPP/logs/error_log
httpd.conf:
php_value error_log /Applications/XAMPP/logs/error_log
i even set up an .htaccess file in the root directory of one of my virtual hosts:
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
php_flag log_errors on
php_value error_log /Applications/XAMPP/logs/error_log
but then every time I reach a statement like this in the code:
die("I love Prestashop");
the code just dies without any output.. any ideas guys?
I got the site running on my localhost using XAMPP and I'm running a Prestashop website, with PHP 5.3.1
Calling die() should not produce output in the error_log as calling it is not an error.
It is just a PHP construct (alias to exit()) that exits the current script and sends (optional) output (or an error code that is useful when used in a CLI environment).
die() or exit() is not useful for reporting errors. They cannot be caught and cannot be logged. Use trigger_error() instead.
Can I hide the path in php error using .htaccess
Example:
Notice: Undefined variable: hello in C:\xampp\htdocs\mysite\index.php on line 3
I want to hide the path using .htaccess or print something let me know if there is an error without print the path of the page:
Notice: Undefined variable: hello on line 3
or
Notice: Undefined variable: hello
or
There is error in your page
Edit :
I put this lines in my .htaccess
But I can't access to my site. There is error "Internal Server Error"
How Can I fix that
# supress php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0
PHP’s error messages are not meant for users but for developers only.
So for a production environment, you should disable display_errors to avoid information disclosure:
Note: [display_errors] is a feature to support your development and should never be used on production systems (e.g. systems connected to the internet).
Instead, you should show generic error messages to your users that do not unveil anything of the internals and only log the error messages (see log_errors and error_log):
Note: You're strongly advised to use error logging in place of error displaying on production web sites.
And if you really want to modify PHP’s error messages, you can use set_error_handler to set a custom error handler.
See also OWASP’s Development Guide on “Error Handling, Auditing and Logging” for further information.
You cannot change the messages : those are generated by PHP, and that's the way they are.
But you can prevent them from being displayed to your website's users -- and still log them, for your own usage.
For that, see :
display_errors : to prevent errors from being displayed to the page's output
log_errors : to indicate that errors should be logged to a file
error_log : to specify to which file errors will be logged to.
Of course, that doesn't prevent you from fixing as many causes of notices / warnings / errors as possible ;-)
Get rid of all that useless stuff.
php_flag display_errors 0
alone will be enough.
If error persists, check server's error_log for the error message
Here is my code:
echo 'foo';
error_reporting(E_ALL);
echo 'this line doesnt end in a semi colon'
echo 'i should get an error here';
When I run this I get no error.
Not sure how this can be?
ini_set('display_errors', 1);
Do note though that if you do this in the file that has the syntax error, it won't work, as it'll never get executed then. You can also set this true in php.ini (not recommended for production servers), or if you use Apache, in .htaccess with:
php_flag display_errors 1
error_reporting directive won't help you to show error messages on-screen. It's responsible for which error to show, not where.
if your PHP runs as Apache module (most likely it does) add the following line into .htaccess file:
php_value display_errors 1
when you switch to production, change it to
php_value display_errors 0
php_value log_errors 1
and watch them it in the error log.
Do you have any kind of shutdown hooks, error-handling functions or global exception catchers running?
Syntax errors can be quirky in large frameworks :)