PHP won't show any errors - php

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 :)

Related

PHP 7 Script not display errors, or writing them to the log

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 ?

Show error 500 when php fails

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.

I can't disable PHP errors

So what's going on is I tried
ini_set('display_errors', 'Off');
error_reporting(0);
Right below <?php, but this didn't seem to stop displaying them. So I went to the php.ini and went to display_errors and saw that it was set to Off. But it still showed.
So I went and did phpinfo() and display_errors along with display_startup_errors are both off. Also html_errors is off. I'm not sure if this will help, but it says error_reporting is set to -10241. Any ideas?
Do not change the value of error reporting to solve the issue. If display_errors is off, errors are not display independently of the error_reporting setting. This way you will not display errors but you can still log them.
The following should work:
ini_set('display_errors', 'Off');
If it doesn't work it could be that your server configuration does not allow you to change settings from PHP scripts. ini_set() returns FALSE on failure. So first of all you should check what value that call is returning. Make sure that ini_set is not listed among disabled PHP functions (disable_functions in php.ini).
If you are asking yourself why errors are still being displayed even if in php.ini the display_errors is Off, you can check the actual value of display_errors during the script execution:
ini_get('display_errors')
Pemember that PHP settings could be changed also in Apache host configuration and in .htaccess files. So check if you have an htacces that enables display_errors. Something like this:
php_flag display_errors on
Try to use:
ini_set('display_errors', 0);
ini_set('display_errors', false);
You don't describe what the errors are, so it's possible that your web server (Apache, nginx, etc) are what's throwing the error and not PHP.
If it is PHP, ensure that you're editing the correct php.ini as identified in your phpinfo.php. Remember that if you edit the php.ini, you will need to restart your PHP process (for example, on some *nix systems: service php-fpm restart. Your exact command may vary.)
If it's off in your php.ini, my guess is that it's being overridden somewhere else -- either later in the script ('grep "ini_set" /path/to/project/*.php' will find it). Also, the PHP Manual states that if the script has fatal errors, it doesn't apply if there are fatal errors:
Although display_errors may be set at runtime (with ini_set()), it
won't have any effect if the script has fatal errors. This is because
the desired runtime action does not get executed.

Errors not displaying in PHP 5.3

I recently upgraded to PHP 5.3.22 and now I'm getting WSODs whenever there are errors in my php code. I know I have display_errors disabled in php.ini, so I tried adding the following code to the top of my scripts to temporarily enable displaying errors on the screen for debugging.
error_reporting(E_ALL);
ini_set('display_errors', '1');
The above works if I have an undefined function but if I miss a semi-colon at the end of a line, it still displays a WSOD.
How can I get all errors to display on screen when I'm developing the scripts?
Enabling error reporting at run time like that fails to show fatals. You can enable it in your php.ini or add this to your htaccess to override it:
php_value display_errors 1
As the file cannot be parsed, setting the error level and display_errors inside the file is having no effect.
Set it in your php.ini

Error logging with WAMP server in PHP

I have a WAMP 2.2 server running on a Windows 7 box and cannot get PHP error logging working at all.
The file is always blank even after I explicitly trigger USER_ERROR errors, or cause normal ERROR errors.
I'm including the error relevant sections of the php.ini file - hopefully you can find something:
error_reporting = E_ALL
error_log = "c:/wamp32/logs/php_error.log" ;(UNCOMMENTED BY ME)
log_errors = On
display_errors = On
The line ; log_errors is just a comment for the following block, for the purpose of showing you what the settings are in dev vs production. You uncommented four lines which aren't meant to control anything, and I'm surprised your Apache service doesn't have problems starting up because of it.
What you need to do is look for the line:
log_errors = Off
And change the value to On
That said, once you restart the Apache service, the settings should take effect. However, I was unable to get WampServer to properly log php errors despite these settings. Apache will not start up when I specify the error_log parameter.
For me it turned out to be a permissions error. I ended up giving EVERYONE full control of the error log file and it seemed to fix my issue. Best of luck.
Did you try adding these lines to your php file?
ini_set("display_errors", "1");
ini_set("log_errors", "1");
ini_set("error_log", "/wamp64/logs/php_error.log");

Categories