Jump to content

How Can I Add A Directory To Php Includes


sylvest

Recommended Posts

I would like to add a directory (e.g. /home/myusername/include) to the PHP include search path. Is there a way to do this globally for the whole of my site? I want to do this in a way that works whichever directory or sub-directory my PHP files are in (i.e. I don't want to have to change my include statement in each web page to give the relative path from where the page is to where my include files are). I would also like to do it in a way that's independent of the account being used or the file structure of the hosting system (i.e. I don't want to have to include an absolute path from / each time because this would have to be changed to work on a different site, or on a different hosting system). I've tried adding a .htaccess file in my public_html directory containing the line

php_value include_path ".:/home/myusername/include:/usr/lib/php:/usr/local/lib/php"

but this gives a 500 error.

Is there a way of making this work?

Or can I add a PHP.ini file somewhere?

Or is there a way of referring to the location of my home directory (_not_ my web root, since I'd like to keep my include files secure) in a PHP include statement?

 

Thanks - Rowan

Link to comment
Share on other sites

As the server is running php as suPHP, you can use local php.ini file for specifying php include directory. Just create a local php.ini inside the root of your account (/home/user) and add the line

 

>
include_path =  ".:/home/user/include:/usr/lib/php:/usr/local/lib/php"

 

And add the following line on .htaccess inside /home/user/public_html

 

>
suPHP_ConfigPath /home/user

Link to comment
Share on other sites

As the server is running php as suPHP, you can use local php.ini file for specifying php include directory. Just create a local php.ini inside the root of your account (/home/user) and add the line

 

>
include_path =  ".:/home/user/include:/usr/lib/php:/usr/local/lib/php"

 

And add the following line on .htaccess inside /home/user/public_html

 

>
suPHP_ConfigPath /home/user

Many thanks - this works.

 

For interest, what exactly does the suPHP_ConfigPath command do? Does this tell suPHP to look for a php.ini file in this directory? If so, does this disable anything you've got in the global php.ini file, or does it just run both files?

 

Thanks - Rowan

Link to comment
Share on other sites

  • 3 weeks later...

As the server is running php as suPHP, you can use local php.ini file for specifying php include directory. Just create a local php.ini inside the root of your account (/home/user) and add the line

 

>
include_path =  ".:/home/user/include:/usr/lib/php:/usr/local/lib/php"

 

And add the following line on .htaccess inside /home/user/public_html

 

>
suPHP_ConfigPath /home/user

 

Does this need to be done for each web accessible subdirectory? This seems to have worked for scripts in the public_html folder, but not for php scripts in folders one level down.

Link to comment
Share on other sites

Does this need to be done for each web accessible subdirectory? This seems to have worked for scripts in the public_html folder, but not for php scripts in folders one level down.

 

Never mind. I could have sworn I created the php.ini file in the root directory but it looks like I placed it in the public_html folder. I moved it up one level and now it's working.

Link to comment
Share on other sites

  • 5 months later...

No, the setup provided above with php.ini is applicable to your entire account where as anything on the .htaccess can penetrate two levels only

 

I can't seem to make this work, I think I followed the step by step exactly, however when I attempt to put a require statement into a php file "require("functions.php")or die(mysql_error());" and have functions.php in the same directory

 

I get the following error message "Fatal error: require() [function.require]: Failed opening required '1' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/fournine/public_html/PHPMySQL/connect_db.php on line 3"

 

If I undestand the instructions correctly the file structure should look something like this

 

/ should contain php.ini

/public_html should contain the modified .htaccess file

/public_html/PHPMySQL/ should contain the page calling the function "connect_db.php" and the functions file "functions.php"

 

 

Couple of questions

 

1. In include_path = ".:/home/user/include:/usr/lib/php:/usr/local/lib/php" do I use the generic /home/user/ ... or do I use /home/my_user_name/... ?

 

2. Same question for the line in .htaccess do I use suPHP_ConfigPath /home/user .... or suPHP_ConfigPath /home/my_user_name ... ?

 

3. Do I need to create an /include directory at the top level and stick my functions file in it, or can I simply have the functions file in the same directory as the file that calls it?

 

Thanks in advance

 

jayr

Link to comment
Share on other sites

Whenever you're referring to a path in your website, "user" means to substitute your_user_ID at that location. Each user on a server has their own "home" directory, which when written as a Linux filepath is /home/userID/

 

However, "usr" should be left as-is in the examples above. That's a different directory.

Link to comment
Share on other sites

Whenever you're referring to a path in your website, "user" means to substitute your_user_ID at that location. Each user on a server has their own "home" directory, which when written as a Linux filepath is /home/userID/

 

Thanks, I had tried doing it with my_user_name before, but I tried doing it again double and triple checking everything, and this time it worked. Also it turns out that I can stick my functions file in the same directory as as the file that calls it.

 

this structure works

 

/ should contain php.ini

/public_html should contain the modified .htaccess file

/public_html/my_working_directory/ should contain the page calling the function "connect_db.php" and the functions file "functions.php"

 

jayr

Edited by jayc
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...