Link to home
Start Free TrialLog in
Avatar of Alex Porter
Alex PorterFlag for United States of America

asked on

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
Avatar of jericotolentino
jericotolentino
Flag of Philippines image

Hi,

If you happen to have another include or require statement near where you want to insert it, just place it there.
ASKER CERTIFIED SOLUTION
Avatar of _Marcel_
_Marcel_
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marcus Bointon
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.
Avatar of gerodim
gerodim

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...
Avatar of Alex Porter

ASKER

Thanks for the help!