Nightmare of PHP programmer: include_once fails

For a moment it was looking like our project got haunted. Suddenly we started getting errors from the libraries being included twice, function exists and class exits kind of error. First few times it was written off as a “honest” mistake and fixed on spot, but it continued to happen again and again. Of course, it was only showing up on production sites, none of the test sites had this issue.

Some digging in the code and pulling hairs out lead to the issue: include_once was failing us. Yes, it was doing include of the same file again! One little detail – that only happened when we used both relative and absolute include path, something like:


include_once("../includes/fancyLibrary.php");
and
include_once("/absolute/path/to/document/root/includes/fancyLibrary.php");

Of course that should not be an issue, according to the PHP documentation.
When I started troubleshooting those includes calling

realpath("../includes/fancyLibrary.php");

the truth popped out – realpath was not returning anything. And then we found why – there was missing “read” attribute on the document root directory, it only had executable bit set (711). Changing permissions to 755 (added read attribute’s bit) fixed the issue.

So, problems with realpath or include_once PHP functions? Check folders permissions!