PHP 5.6, find an explanation for seemingly unexplainable errors - php

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.

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

My dynamic site is not loading when the site is online but works well in localhost

I'm getting this error when my site is online
[host www.sitename.com] Backend fatal error: PHP Fatal error: require_once() [function.require]: Failed opening required 'admin/config/connection.php' (include_path='.:/opt/cpanel/ea-php53/root/usr/share/pear:/opt/cpanel/ea-php53/root/usr/share/php') in /home/mistrybu/public_html/index.php on line 373\n
The site works well in localhost but when its put online , the dynamic part is not showing.
I changed the php version from php7 to php5 in cpanel. Now the front end is working but the backend is not.
Can someone help me solve this error?
PHP version
Definitely do not go back to PHP5. Keep it to 7. If that means rewriting some of the existing code, so be it, it will make your site more secure and more future proof.
Error Message
Your require_once() method cannot find or access a particular file. This is most probably because:
File missing
The file (connection.php) doesn't exist in the given location (admin/config). Now keep in mind that as the location doesn't start with a /, it's going to be relative to where the require_once() method is being run from.
Since you're running require_once() from:
home/mistrybu/public_html/
it's going to look for the file here:
/home/mistrybu/public_html/admin/config/.
Does that location exist? Does the connection.php reside in the folder?
Folder mismatch
Try adding the following prefix to your path, and see if that makes a difference:
require_once $_SERVER['DOCUMENT_ROOT'].'/admin/config/connection.php';
File errors
Maybe the file exists, but for some reason, gives an error. Try running the following from your command line and see if you get any errors:
php -f admin/config/connection.php

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.

simplexml_load_file(): I/O warning : failed to load external entity "/user-bundle/Resources/config/doctrine/model/User.orm.xml

I have some issue with my production deployment of Symfony2,
I've tried many solutions, but none have worked.
I randomly have this error when accessing my symfony application on production environment:
( ! ) Fatal error: Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException' with message 'Warning: simplexml_load_file(): I/O warning : failed to load external entity "/home/user/symfony/vendor/friendsofsymfony/user-bundle/Resources/config/doctrine/model/User.orm.xml"' in /home/user/symfony/app/bootstrap.php.cache on line 2998
( ! ) Symfony\Component\Debug\Exception\ContextErrorException: Warning: simplexml_load_file(): I/O warning : failed to load external entity "/home/user/symfony/vendor/friendsofsymfony/user-bundle/Resources/config/doctrine/model/User.orm.xml" in /home/user/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php on line 736
Call Stack
# Time Memory Function Location
1 0.0000 262880 {main}( ) ../app_dev.php:0
2 0.0015 572736 Symfony\Component\HttpKernel\Kernel->handle( ) ../app_dev.php:79
3 0.1342 4023952 Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle( ) ../bootstrap.php.cache:2376
( ! ) LogicException: Request stack is empty in /home/user/symfony/app/bootstrap.php.cache on line 2998
Call Stack
# Time Memory Function Location
1 0.3330 7110120 Symfony\Component\Debug\ErrorHandler->handleException( ) ../classes.php:0
2 0.3331 7119696 Symfony\Component\Debug\ErrorHandler->handleException( ) ../classes.php:1939
I've tried to upgrade my php version (I was in php 5.4.x and now in 5.6.4),
I've tried to upgrade lixml2 version (i am in 2.8.0 now, but i already tried to upgrade in 2.9.3)
I've constated that the version of libxml used in php is always 2.8.0, but, I haven't found the way to change this ,
I've tried to set the all directory of symfony in chmod 777
My server is a debian 7.5 server.
Maybe someone who knows this error can help me
Here is some links to differents question related to this one:
Random Error, FOSUserBundle Error and Service error
I didn't post in them because they're all outdated
[EDIT]
I found a quick fix, but it's in vendors, so it will be overrided in the first update of the doctrine update:
QuickFix in XmlDriver.php Line 737
$xmlElement = #simplexml_load_file($file);
if(!$xmlElement){
$xmlData = file_get_contents($file);
$xmlElement = simplexml_load_string($xmlData);
}
We got this error after we started using libxml_disable_entity_loader(true); in our code. That code is vital in preventing XXE -attacks(more info of that in here). If you don't have that in your code, it could be that you have installed/updated a bundle which has that line of code in use. Note that libxml_disable_entity_loader() is not thread safe, so if there is one piece of code on one thread that executes that line, everything on that server now has that enabled throughout the process.
The FOS-bundle seems to use xml-definitions, that in return have external entities in them. Nothing major though, but that code prevents the FOS-bundles methods from using those files correctly.
Luckily, our service got that error only in one place, and the fix was obvious:
Add a libxml_disable_entity_loader(false); before executing that piece of code where the error comes from, and add libxml_disable_entity_loader(true); right after that piece of code.
This way the user bundle could load the xml:s it needed, but security is not compromised.
Example:
libxml_disable_entity_loader(false);
$user = $query->getOneOrNullResult(); // This generates an error if entity loader is disabled
libxml_disable_entity_loader(true);
The error mentioned usually happens when the xml file does not exists.
You can try check if the file really exists after try to load:
if (file_exists($file)) {
$xmlElement = #simplexml_load_file($file);
}
I tried to simulate the error locally running php interactively, the results are in down:
php > simplexml_load_file('test.xml');
PHP Warning: simplexml_load_file(): I/O warning : failed to load external entity "test.xml" in php shell code on line 1
php >
Note, the error above is the same that you mentioned on the question. So, the first try is use the file_exists() function to check if the file really exists after try to load it. The next step is check if the current path - using getcwd() - and the path of the file are pointing correctly to the file.
I created a file called test.xml on /tmp folder:
<test>
<worked>
</worked>
</test>
Called the interactive mode on php:
$ php -a
Interactive mode enabled
So, checked my local directory:
php > echo getcwd();
/tmp
And, tried to load the file:
php > $data = simplexml_load_file('test.xml');
php > var_dump($data);
object(SimpleXMLElement)#1 (1) {
["worked"]=>
object(SimpleXMLElement)#2 (1) {
[0]=>
string(2) "
"
}
}
Note: that worked correctly. So, probably, your file does not exists or the path are incorrect. The path considers the current path /tmp plus, the file indicated on the function call test.txt, or equals to call the /tmp/test.txt file.
In my case, the problem on new hosting was:
allow_url_fopen
was disabled (any simplexml_load_file() or file_get_contents() returned false).
Solution: enable allow_url_fopen.
There are some solutions given:
https://github.com/FriendsOfSymfony/FOSUserBundle/issues/1062
https://github.com/symfony/symfony/issues/7291

Categories