php error log for class files in different directory - php

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

Related

PHP 5.6, find an explanation for seemingly unexplainable errors

someone could tell me if there is a reason why I get these PHP errors, which should not exist.
PHP Warning: include (xyz.php): failed to open stream: No such file or directory in /mypath/myfile.php on line 351
When the file clearly exists!
PHP Fatal error: Can not access MyClass::$myproperty in /mypath/MyClass.php on line 250
When the declaration exists:
class MyClass
{
...
public static $myproperty = array();
...
I would like to emphasize that these errors are not always reported! But only once in a while. When I make a mistake writing a part of the code, the PHP fatal error should always occur, not 5 times a day from 12:22 to 12:27 and nevermore. I'm talking about a site from thousands of visits a day, I'm also talking about a .php file that is included on a regular basis in every request of any page.
I can't get any error in my pc (php 5.6 also) when I run and test the web application.
Is it possible some server problem?
Thanks.

Php code executes twice and causes "Constant allready defined" errors

Recently my aplications started throwing errors of this kind:
PHP Fatal error: Cannot redeclare {X Class} (previously declared in {X File: X line}) in {X File: X line}
After some investigations, I came to the conclusion that the PHP code is somehow being executed twice.
To make sure, I created a file with the following code:
error_reporting(E_ALL);
define('SOMETHING', 'ITS OK');
echo SOMETHING;
die();
That is the entire code of the file. Yet sometimes, when requesting that file, the following error is beeing produced:
PHP Notice: Constant SOMETHING already defined in {Y File} on line 6
Do you know what kind of setting could be causing this?
The issue seems easyer to reproduce with fast consecutive requests but it might not be limited to that scenario, sometimes it seems to happen on a single request (but Im not considering this a fact)
The issue seem to appear because of proactive defense in imunify360. https://www.imunify360.com/proactive-defense . Disabling the proactive defense php module seems to resolve the issue.

PHP 7 include file error - file name is parsed incorrectly

Recently I met a weird error and hope someone can help.
We use LiteSpeed WebServer and Nginx on top, with PHP 7.5 and MySQL 5 (well, WordPress is the framework). It runs well for many days.
Suddenly it goes fatal at this line in different files
include ("class-xxx.php");
The error log says
[02-Oct-2018 13:04:36 UTC] PHP Fatal error: require(): Failed opening required 'dlass-xxx.php' (include_path='.:/opt/cpanel/ea-php72/root/usr/share/pear') in /xxx.php on line N
I double checked the code and see that the file name is declared "class-xxx.php", but somehow the server tries to include "dlass-xxx.php", thus it cannot find the file and trigger fatal error.
Solution? I re-upload the file, it works. But then it goes fatal in another file, at similar lines using include.
I notice that the first character in the file name, for example "c", is read incorrectly and shift 1 byte before (or after), so the file name is correct.
Such as
include "class-xxx.php" is parsed/read as "dclass-xxx.php"
include "page-xxx.php" is parsed"read as "opage-xxx.php"
...
this is very strange. Do anybody know the reason? Is it related to any caching/memory management of LiteSpeed or Nginx or PHP 7. ?
It happens on PHP 7. only because if I switch to PHP 5.6, the error does not happen.
Hope someone can help. Thanks a lot.

require_once raise HTTP ERROR 500

I'm working in a team on a e-shop website.
Recently, we've passed on Debian Jessie and from php5.4 to 5.6
Since then, we have multiple bug with require and require_once
Here's a file that raise an ERROR 500
<?php
require('/data/vhosts/mycompany.com/public_html/mp/../includes/functions/marketplace_tools.php');
//print_r(get_included_files());
require_once('/data/vhosts/mycompany.com/public_html/mp/../includes/functions/marketplace_tools.php');
echo 'pouet';
?>
The error.log say this :
PHP Fatal error: Cannot redeclare match_marketplace_products() in Cannot redeclare match_marketplace_products() on line 1
The filepath is good.
Marketplace_tools.php don't have require/include in
Marketplace_tools.php is just a bunch of function we use
If I run this file in command line, it works well
If I uncomment the print_r, it does not raise ERROR 500
This bug randomly appear and disapear
Cannot redeclare match_marketplace_products()
This error message tells you that there is a function name (match_marketplace_products) in the file you're including that already exists in the current PHP program.
You're using require_once(), but check that you haven't pulled in the same file elsewhere using require() (or include()).
Also check that the function name match_marketplace_products is only defined in this one file -- if it's defined in another file that is also included, then this will cause this error.

Disabling PHP Error Messages

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.

Categories