Include/require work but include_once/require_once suddenly do not

Several of my PHP (7.2) apps stopped working this morning if they use include_once or require_once, but they worked fine yesterday. The same code also works fine on the same server, but other domains with minimal difference in vhost configs and no htaccess files (Apache 2.4). Also, things work if I chase down the error and remove “_once” from everything. I’ve been using PHP for almost 20 years and never seen anything like this. The error I get looks like the following, but it happens for any include_once/require_once.

include_once(): Failed opening ‘/whatever_path/whatever_file.php’ for inclusion (include_path=’.:/usr/share/php’) in /another_path/another_file.php on line 24

PLEASE HELP!

Sounds like the path is different, not that the call is wrong.

But it works fine if I do

include("/whatever_path/whatever_file.php");

Just not

include_once("/whatever_path/whatever_file.php");

That is odd. My first thought was that the file is already included and that would prevent it from including it again, but that wouldn’t throw the error…

Right. I thought that too, but as you said that wouldn’t result in an error.

Php has a reoccurring history of the _once() statements not working due to not being able to normalize and figure out the real path of files. This however is php version specific. Has your php version been changed by you or your hosting?

There should be a second error message before the one you posted stating why php thinks the file cannot be included/required.

Is there a reason you are not using just ‘require’? The _once() versions are actually slower because they must examine the list of already included/required files.

There is no second error. I do realize the _once versions are a little slower, but it isn’t substantial for our environment. We are using it largely because some of the third-party code we have uses it, such as SimpleSAMLphp. As I said, I could chase down all the _once’s and remove them, and I actually did that for some critical apps to get them back up, but I have so many apps that I would really like to fix the core issue.

I forgot to mention the PHP version has not changed (7.2). I manage the server myself, not hosted. It is running Ubuntu 18.04 LTS, and nothing has changed since these things were all working yesterday afternoon.

Is php running as an Apache server module or as a CGI application? If a as a server module, the _once() mechanism may have become corrupted or you found a bug, and restarting the web server may (temporarily) clear the problem.

It is running through mod_php. I did restart apache, and even the server entirely. Unfortunately, that did not clear the problem. It may be a bug. We are in the process of eradicating the use of once_ in all of our applications for now. Thanks for trying.

I do not have a quarter of the experience combined by posters in this thread. I am simply adding my own experience which may or may not be helpful.

I had this problem a few months ago. I started using include_once and it worked. Suddenly, i started getting errors like the errors mentioned by the op. I tried many hack jobs and nothing seemed to work except simple include statements:

include '/dir/file.php';
include '../../dir/file.php';

i switched to require with dirname and i no longer have problems:

require dirname(__FILE__) . '/file.php';

i think it may have something to do with file paths and whether or not the file is accessed by other pages. try looking into the path to see if you can retain the include_once code. Otherwise, you may have to switch all of the code to something more stable.

I’ve made a little progress on this. After reinstalling apache, the issue goes away, but only for a few minutes.

sudo apt-get install --reinstall apache2

Sadly, this is only a temporary fix. The issue returns after just a couple minutes.

In fact, I just tried reloading apache, and that corrects the issue for about the same amount of time.

Any thoughts?

Are the paths in the include statements relative (starts with a dot or dot dot) or are they absolute (starts with a / or using $_SERVER[‘DOCUMENT_ROOT’]) ?

What I suspect is they are relative and some code is either altering the include_path or the current working directory (CWD) or they are using $_SERVER[‘DOCUMENT_ROOT’] to form absolute paths and some code is altering that value.

Perhaps examine/log these values and see if they are changing from the point where it ‘works’ for a while to when they don’t.

This seems like a y2k problem, where you came up against some limit in the software/operating system, such as a disk quota, not enough free space, or a maximum file size.

Are there files on the server in question that are being dynamically produced/upload so that there is an ever increasing amount of them or they take up an ever increasing amount of storage?

Along these same lines, the symptom of it ‘working’ for an amount of time after restart, sounds like a memory management, virtual memory, disk swapping, free space issue.

The include_once/require_once statements fail with absolute or relative paths. The system has more than 50% free space. I’m exploring the memory issue, and I’ll let you know. Thanks.

Sponsor our Newsletter | Privacy Policy | Terms of Service