Getting a require function to look in the right place

All of my php files being with the following line of code:

require("/files/required.php");

The file "required.php" initiates session variables, and sets up the database connection.  Location of this file is www.mysite.com/files/required.php.

I recently moved to a new host, however, and that line of code no longer looks in the right place for that file.

My webhost suggested the following code:

$PHPRC = getenv('PHPRC');
ini_set("include_path", $PHPRC . "/home/myaccount/public_html");
require("files/required.php");

This works, but I wish I didn't have to change the code on all of my files.  Is there an easy way to update this site wide?  I feel like I should be able to edit my include directory or something like that in php.ini, but I'm not clear on whether I can just post a php.ini file into my account or exactly what code to change in the sample php.ini from php.net.  Also, how can I make this work with the code including the forward slash at the beginning of the location?  The way it was at my last host, this code would find the file regardless of relative location.  

Here's the code again that's at the top of my files:

require("/files/required.php");

Thanks for the help!
Alex
alexswebsiteAsked:
Who is Participating?
 
_Marcel_Commented:
You could of course create a .htaccess file in your document root with the following line of code:

php_value auto_prepend_file /the/path/to/your/documentroot/setroot.php

Then create a file called 'setroot.php' that changes your include_path (like suggested by your webhost):

<?
$PHPRC = getenv('PHPRC');
ini_set("include_path", $PHPRC . "/home/myaccount/public_html");
?>

This script will automatically be run before all your scripts and it will fix your document root.
0
 
jericotolentinoCommented:
Hi,

If you happen to have another include or require statement near where you want to insert it, just place it there.
0
 
Marcus BointonCommented:
First of all check what your include path is set to. If it includes '.', then you should be able to just use require 'files/require.php', which will be a simple global search and replace. Using an absolute path for includes has its hazards, as you are finding.

You can override a global php.ini setting in a .htaccess file:

php_value include_path ".:/home/myaccount/public_html"

Though that may break paths pointing to things like pear repositories.
0
 
gerodimCommented:
At first try using (.) .
E.g.
require("./files/required.php");

If it doesn't work try using the whole path in the server....
Print out a phpinfo() and find your directory on the server so u can write the full path...
0
 
alexswebsiteAuthor Commented:
Thanks for the help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.