Disabling PHP Error Messages - php

I am working on my own custom error handler function. Currently I am trying to disable error reporting so it wont display errors to public users. However it is not working.
<?php
// Disable error reporting.
ini_set( "error_reporting", 0 );
// Create an error.
echo "hi
?>
This returns this error:
Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in C:\xampp\htdocs\websites\sicsportsagency.com\includes\test.php on line 7
What am I doing wrong?

You can disable error reporting for syntax errors, just for any other sort of error. But there’s a catch.
Using ini_set like you’re doing works for errors besides syntax errors because ini_set (presumably) runs before the code that has the error in it. With syntax errors in the same file, not so. PHP must parse the whole file before it can even begin to execute the first line of code. If it encounters an error in parsing, even if that error is further along in the file, it’s still parsing and not yet executing: it hasn’t had the chance to run ini_set.
The most straightforward solution would be to edit php.ini so the appropriate setting is set before even parsing begins.
If you can’t do that, you might have to have to put the code that has a potential syntax error in another file, and then the original file can ini_set and require the second. Since require will only parse the required file at the point of execution of the require, you can be sure that ini_set will run before any parse error in the required script can occur.

Related

php error log for class files in different directory

Please excuse an, at least initially, high level question about logging php script errors. Is it a valid expectation that php error_log will record errors in a class file that is in a different directory to the main php script?
I have two php files
pages/add_edit_xxxx.php
and
class/class_xxxx.php
add_edit_xxxx.php depends on public functions in the class file
If I deliberately introduce an error in pages/add_edit_xxxx.php then pages/error_log correctly records the line number of the error
If I deliberately introduce an error in class/class_xxxx.php then class/error_log sometimes records the error and sometimes remains unchanged
I do not have access to php.ini
But I guess that the settings are OK as sometimes class/error_log records the error but more often than not it does not log the error.
I have this in a settings file:
ini_set("include_path", '/home/xxxx/php:' . ini_get("include_path") );
error_reporting(E_ERROR);
#session_start();
Called at the start of pages/add_edit_xxxx.php
include_once("settings.php");
I hope this is enough to enable thoughts on whether it is a valid expectation that the errors in the class file should be reliably caught and logged. At the moment I cannot recreate a scenario when a log was created - but this is an example of class/error_log entry that was written yesterday
[20-Oct-2021 19:00:48 UTC] PHP Parse error: syntax error, unexpected '$tempRS' (T_VARIABLE) in /home/xxxxx/public_html/xxxxx.com/class/class_xxxx.php on line 36
If I remove the deliberately inserted errors (for example a missing semi-colon) then the page functions well.
Thank you
resolved by updating this line in the settings file:
error_reporting(E_ERROR);
to
error_reporting(E_ALL);

Error not displaying in PHP

Why can't I see what the Error is. On youtube, when they forgot to put in a ; an error along with which file and which line on that file was displayed. When I get an error, I just get:
Server Error
500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed.
How do I get it to show what the error is?
I think that is a server error, and not a php error, but in your php.ini file towards the bottom and not in the examples, there are error reporting options. Be sure you change them back to no, after the error is corrected, this is a security measure to keep your code private.
As far as a 500 error, check your apache logs.

Error reporting is set to 0 but it is still showing errors

In my application I have error_reporting set to 0 and just to make sure i is set when I echo error_reporting() it says it's 0.
If I leave out a semicolon on purpose just to trigger an error (somewhere under where I have it set to 0) it still displays the error:
Parse error: syntax error, unexpected T_VARIABLE in . . .
Does anyone know why this is happening? I am developing on my own machine using WAMP.
Thanks.
Since it's a parse error, it is encountered before it actually runs any of your code, and so it shows up.
You should change the setting in the php.ini file instead, as this will be handled before parsing the PHP file.
Parse errors are shown unless you actually set error_reporting in php.ini file to 0

Why does Apache crash while executing following PHP code?

$reset_Array=(); // I forgot to put the keyword "array"
Correct way should be
$rest_Array= array();
Why does Apache crash when I try to execute wrong code.
What is happening internally?
I don't think that Apache crashes. What happens is your PHP execution aborts, resulting in you seeing 500 Internal server error. This is due to the fact that syntax
$reset_Array=();
is invalid in PHP. PHP is trying to parse this line and encounters an error. It returns this error and the execution aborts. Try the following: put in a new file test.php the following:
<?php
$reset_Array=();
?>
And execute this with a command-line interpreter with -l parameter (lint - syntax checking):
$ php -l test.php
You will get the following error:
$ php -l publish/test.php
PHP Parse error: parse error in test.php on line 3
Errors parsing test.php
Once PHP encounters this error, it cannot continue executing the script, because it cannot parse it. Hence you receive an error when you try executing it under Apache.
P.S. The above commands are shown from unix/linux shell. If you are running under windows, then your prompt may be something like C:\Documents > instead of $.
Apache isn't crashing. It's just no errors are being displayed.
You could look at the errors in the error_log file, which resides in the same directory as your php script.
What you could do is look in your php.ini file for the uncommented line error_reporting = foo. Change that foo to E_ALL.
Then it should show the errors, instead of just simply so-called 'crashing'.

why does this syntax error return HTTP error 500 when 'display_errors' is on?

given the following script
<?php
ini_set('display_errors','On');
error_reporting(E_ALL);
thisisanerror
?>
I get the expected
Notice: Use of undefined constant error - assumed 'error' in /htdocs/test.php on line 8
but if I add something to the script
<?php
ini_set('display_errors','On');
error_reporting(E_ALL);
error
function test () {
echo('test');
}
?>
I get
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
Why am I getting a 500 error instead of a normal syntax error in the latter case? Shouldn't display_errors always display the error?
The second code sample is an outright syntax error. This means PHP will not progress to the point of code execution and will die at parse time. Since no code is executed, the effects of the ini_set() call are not seen, so you do not get the PHP textual error indicating the problem.
A parse error is fatal, and the web server is (rightly) set to handle PHP fatal errors with a 500 response code. Since your PHP script did not produce any output the web server will return it's default error document for the 500 condition.
If you want to see the textual message for parse errors in the browser - and one wouldn't normally do this is production, by the way - you will need to turn error reporting on in php.ini or using a .htaccess file.
If forcing php to show error on run time doesn't work for you, you may try other options like setting it in php.ini instead:
error_reporting = E_ALL | E_STRICT
display_errors: On
Good Luck!

Categories