We help IT Professionals succeed at work.

500pts: Set includes path for cgi PHP without editing php.ini

Medium Priority
Last Modified: 2013-11-18
Hello all,

I have PHP5 installed as a cgi.  And I have a website that is hosted in a sub-folder among many others within a hosting account.  This website is using PHP.

I want to set a constant includes folder pat so that I don't have to worry about the path as the site gains more and more levels.  BUT, because this is not the only site hosted in this hosting account (each in their own folders with domain pointers), I DON'T want to set the include path in the php.ini file, otherwise I have to put all includes for all sites in the same folder.

My understanding from reading is that include path can be set using htaccess only when php is installed as a module, not as a cgi.  

Is there some other way I can achieve what I want?

Watch Question

What about using the:

// Works as of PHP 4.3.0

Sets the include_path configuration option for the duration of the script ..

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
the above was just an example where inc is the path:

string set_include_path ( string new_include_path )


Thanks for the idea.  

I need to be more clear: I don't want to have any reference to the path to the includes in the page's file.  If I have 200 html pages in my site scattered through three levels of sub-directories, I want to be able to just put

<?php include "my_include.txt" ?>

and have it go to one central include directory regardless of which level of sub-directory the page was in.

If I use the set_ini() function, I still have to incorporate that into each page file.

Basically, I want something that worls like an htaccess include path declaration - that is, "for every php file from here down, use xyz as the include folder path".


Wait a minute - maybe I made a dumb reply.

set_ini() could work [b]IF[/b]

a) the path can be set NOT relative to the directory of the page running the script, but as an absolute path, perhaps from the account root ora url

b) the code will not show up in the source of the rendered page; that is, there is no way for users to ever see it.

Then I could paste the exact same code into the header of every page file.  It's not glorious - if I ever migrate the site to its own hosting account where it would be simpler to just edit php.ini, I'll have to go through each and every page file to remove that snippet.  But it might do in a pinch for now if it can work like that.


Of course that's set_include_path(), not set_ini().
Why even bother with trying to reset the include directory?  That actually introduces more potential issues.

Just use an absolute path.  I don't know, something like:

<?php include(_SERVER["DOCUMENT_ROOT"].'/inc/my_include.txt'); ?>

No matter what server you move to, so long as that server var is properly filled, and you put /inc directory in your public html root, you are good to go.

Nothing from the include statement should make it to the html.  do @include if you are concerned about a possible error making it to the user, the @ will eat it.



Also an interesting idea.

But now, if I ever want to change the location of my includes directory, I have to go back and edit every single web page file.

IDEA - can I set a programmer-defined variable in the php.ini file?  Can I define my own $perso_includes_path variable in the php.ini file, and then refer to it in php code, like

<?php include($perso_includes_path.'/inc/my_include.txt'); ?>

That way, if I move things around, I have only that one line to edit.
I would preferably make a variable like this

$include_path = "($_SERVER["DOCUMENT_ROOT"].'/inc";

in your main function file, or header file, atleast something that is called in every file.

then in your files you can only type


this way if you wanna change the folder, you only need to do this one place.
hope this works for you :)
err, minor edit from my first post

$include_path = "$_SERVER["DOCUMENT_ROOT"].'/inc";
"if I ever want to change the location of my includes directory"

just don't. ;)  but really, pick a location you can live with.  In the worst case, a script could be written to modify that line in all other scripts.  I don't know of anything you could pre-set in php.ini (though, if you could pre-set your php.ini, you could change your default include path, and this would all be moot!).

If you want to indirect it a little, you could do something like:

<?php include(_SERVER["DOCUMENT_ROOT"].'/root.inc'); ?>
<?php include($perso_includes_path.'/inc/my_include.txt'); ?>

where /root.inc sets $person_includes_path and nothing else.  THEN, in the worst case, you'd have to have a root.inc file even if you moved the location of /inc to somewhere else, but one file with one line might be worth it if you are unsure of a path.



Goahm I hate to be difficult, and myabe I'm missing something, but if I put

$include_path = "($_SERVER["DOCUMENT_ROOT"].'/inc";

in my "main function file, or header file, atleast something that is called in every file"

then i STILL have to include the path to that main function include, hardcoded into each web page file.  No?


Davebytes is closest to the mark so far.  Creative idea, I like it.  I can see this root.inc file being an opportunity to set all kinds of site-wide variables.'

Along those lines, I found this:


"Template Loading INIs

Lastly, when HW3 is parsing through a template for outputting, the template can make HW3 load an INI file as well. If while outputting the template, if HW3 comes upon "%%CONFIG=xxx%%", then HW3 will attempt to load the xxx.ini.php file if it exists.

This is very useful to load a set of user defined variable presets. Thus at the beginning of your header you could load an INI file which held user defined variables which define the font faces, colors and other various settings"

But I'm not sure if this will work for me, or how what they refer to works.
unfortunatley yes, didnt think about it too much before i answered,
what i have been doing is that i include my function file in each file, and every other file that is included in my scripts uses that variable.
Pretty much just what davebytes said, and that kinda leaves you back to where you started


Everyone sure I can't just define my own variable in the php.ini file and then refer to it?
I have never been altering my php.ini file, so i really cant help you with that, but look at:


too see if you can get some ideas


OK, here's about as good as I can imagine using combinations of ideas, but especially davebytes'.

Instead of putting the this root.inv file at the document root, put it in the default includes directory.  That way, I don't have to clutter up the root with extraneous php files, AND the include statement for this file can be path-less, since php will look first in the default directory.  Then, in that file, re-set teh default includes path.

Lokks about as close as I'm going to get.


Tried it - yep, that does it.  Now on my site I just have to include a one-liner set_ini() in the default includes directory, paste an include at the top of the html page template referencing this file with no path, and I can set the includes path for the whole site.  Not once do I have to hard code a path into any web page file.  And neither do I have to  clutter up the ftp space with extaneous files.  It;'s all contained where it was meant to be contained.

Thanks for the help everyone.


So, I;ve split the points betwen Dave & Chokehold, since my final solution is a combinatino of their two ideas.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.