Home » Php » include – PHP Fails on require_once

include – PHP Fails on require_once

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’ve got a PHP script that includes ( or ‘requires’ ) a set of other scripts. This is effectively for importing all of my classes. What I’m encountering is an HTTP 500 error. I’ve sifted through and commented out code piece by piece to determine that it’s failing on the require_once in one of my files.

Here is the code:

index.php:

<?php
require_once("std/classes.php");
?>

And std/classes.php:

<?php
RequireStandards();
RequireAddons();

function RequireStandards( )
{
    $ClassFiles = scandir("classes/standard");

    foreach( $ClassFiles as $ClassFile )
    {
        if( $ClassFile == "." || $ClassFile == ".." )
            continue;

        //require_once("classes/standard/" . $ClassFile );
    }
}

function RequireAddons()
{
    $ClassFiles = scandir("classes");

    foreach( $ClassFiles as $ClassFile )
    {
        if( $ClassFile == "." || $ClassFile == ".." || $ClassFile == "standard" )
            continue;

        //require_once("classes/" . $ClassFile );
    }
}

?>

This code will work as it sits, but as soon as I uncomment the requires, it fails. What strikes me as odd is that I have plenty of other sites on this server that act in almost an identical manner.

I feel as if I somehow have my PHP error reporting turned off… which I don’t know how to turn back on; as I just upgraded to PHP 5.3. I would typically expect a ‘couldnt open file’ or some such in my browser, if PHP failed.

Maybe someone could tell me why this is kicking back an HTTP 500, or perhaps just how to re-enable error reporting. It would be much appreciated; this just doesn’t seem to make much sense.

How to&Answers:

To enable error reporting:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once("std/classes.php");
?>

Hopefully that should work.

EDIT: If this does work, remember to turn off display errors before putting anything in a live, public facing environment!

Answer:

You can temporarily turn error reporting back on using the error_reporting() function, for example to show all errors put the following code in your file:

error_reporting(E_ALL);

Of course, to change this permanently you should edit your php.ini file, and make sure you have error_reporting enabled as well as display_errors (at least if this is not a production environment). You can also try:

ini_set('display_errors', 1);

Though this may not work if you have a fatal error on the page. Again, to enable this permanently you will have to modify your php.ini file.

It is generally suggested that you enable display_errors only on non-production systems so that users don’t get potentially sensitive information via your error messages.

In either case, you should be able to find the php errors in the apache error log, on ubuntu this is located here:

/var/log/apache2/error.log

Though it may vary based on your distribution.