Jump to content


Photo

How Can I Add A Directory To Php Includes


  • Please log in to reply
11 replies to this topic

#1 sylvest

sylvest

    Family Friend

  • Members
  • PipPip
  • 66 posts

Posted 31 January 2011 - 07:02 AM

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

#2 TCH-Alex

TCH-Alex

    Technical Support

  • Staff
  • PipPipPipPip
  • 663 posts

Posted 31 January 2011 - 07:43 AM

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

Alex Spaford
Technical Support
TotalChoice Hosting, Inc.
Total Choice Hosting - Helpdesk


#3 sylvest

sylvest

    Family Friend

  • Members
  • PipPip
  • 66 posts

Posted 31 January 2011 - 09:37 AM

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

#4 TCH-Bruce

TCH-Bruce

    Volunteer Moderator

  • Members
  • PipPipPipPip
  • 19,960 posts

Posted 31 January 2011 - 12:09 PM

It runs both files. Anything you put in your php.ini file will supersede the system php.ini file.

Bruce Richards
Forum Moderator
TotalChoice Hosting, Inc.
Webhosting by Total Choice Web Hosting - General Support Forum

I am a Forum Moderator. While I can assist in answering most of your hosting related questions, I am unable to answer questions about specifics relating to your account such as billing and server related issues. Should you need assistance in these areas, please contact our Help Desk or our many other options. Another good place to find answers is with our help pages, tutorials and movie tutorials.


#5 SteveW

SteveW

    Distant Family

  • Members
  • PipPipPip
  • 129 posts

Posted 31 January 2011 - 12:21 PM

In addition to what Bruce said,

Does this tell suPHP to look for a php.ini file in this directory?

Yes.

#6 sylvest

sylvest

    Family Friend

  • Members
  • PipPip
  • 66 posts

Posted 31 January 2011 - 01:41 PM

Thanks to all of you for your informative replies. I've now achieved what I was looking for _and_ I've learned something useful. TCH support is superb yet again.

Thanks - Rowan

#7 kf4zmt

kf4zmt

    Family Friend

  • Members
  • PipPip
  • 53 posts

Posted 17 February 2011 - 09:02 PM

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.

#8 kf4zmt

kf4zmt

    Family Friend

  • Members
  • PipPip
  • 53 posts

Posted 17 February 2011 - 09:12 PM

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.

#9 TCH-Carl

TCH-Carl

    Technical Support

  • Staff
  • PipPipPipPip
  • 1,180 posts

Posted 17 February 2011 - 10:57 PM

Does this need to be done for each web accessible subdirectory?


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
Carl Noonan
Techical Support Manager
TotalChoice Hosting, Inc.
http://www.totalchoicehosting.com

TCH Help Desk .. || .. TCH Blog

Posted Image

#10 jayc

jayc

    Family Friend

  • Members
  • PipPip
  • 65 posts

Posted 21 July 2011 - 01:47 PM

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

#11 SteveW

SteveW

    Distant Family

  • Members
  • PipPipPip
  • 129 posts

Posted 21 July 2011 - 08:54 PM

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.

#12 jayc

jayc

    Family Friend

  • Members
  • PipPip
  • 65 posts

Posted 23 July 2011 - 08:41 AM

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, 23 July 2011 - 08:44 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users