Search code examples
phprequire-once

php require_once tries to include a second time on my production server only


I have this code at the top of various include files:

require_once("functions.php");

Sometimes I need to include several of my include files to generate a page and on my local server this works fine as the code above tells it to only include functions.php once (hence it doesn't attempt to declare functions twice).

When I upload to my production server, suddenly it tries to include functions.php a second time, resulting in a fatal error from attempting to redeclare the functions a second time.

Is there something in my php config on my production server that would be causing require_once to behave differently?


Solution

  • You can use the following code:

    require_once(realpath(dirname(__FILE__) . '/functions.php'));
    

    and modify the path location before functions.php if functions.php is located in another directory in root. The function realpath(dirname(FILE) takes path of root folder. So you should include your function in your index.php and modify if your function.php in any directory such as:

    require_once (realpath(dirname(__FILE__) . '/your_directory_name/functions.php'));
    

    Enjoy!!