Forum Moderators: coopster
I have a problem with php includes. I have searched the forums and the web in general to try and find a solution but to no avail (there's a similar post here: [webmasterworld.com ] but it doesn't give a solution). Basically, what I want to do is have a directory outside the web root (public_html) that contains all my include files. I want to set the include_path variable in PHP to the path of includes directory. From what I can gather, there are three ways of doing this:
- setting the value in the .htaccess file using php_value
- having a custom php.ini in my public_html directory
- setting the value at runtime with ini_set
The first method doesn't work, my provider doesn't let me. Neither does the second, and I don't like that one anyway because it would require keeping the php.ini in synch with the main one. For the third one, I thought I could do it in a config.inc.php file, that I call at the start of every file. I put the config.inc.php in my webroot, and I want to call it like this
include($_SERVER["DOCUMENT_ROOT"]."/config.inc.php");
However... the big problem I'm having is that DOCUMENT_ROOT is NOT returning my "public_html" directory, rather the main webroot of the server. My public_html directory is the root of the alias I have in Apache. Is there PHP variable that contains this?
I have a user account on an PHP5/Apache server running on Linux. (I'm not too experienced with Linux so if I say something silly please bear with me!). I develop on a Windows box.
Does anyone have any idea how I can resolve this? By finding a solution to the "DOCUMENT_ROOT" problem or doing it an entirely different way?
Any help is greatly appreciated!
Many Thanks
Dave
if that's the case then assign DOCUMENT_ROOT to a variable and replace the publick_html/ part with the name of your directory.
After that use include($my_new_path."/filename.php");
I hope i understand correctly
I think your solution would work, but what I don't want to do is set a variable in at the top of every file (that's what the config file I want is for), as that means I couldn't move between servers with different directory structures without changing every file.
$_SERVER['DOCUMENT_ROOT'] returns the "DocumentRoot" value from the Apache httpd.conf file. Also in this Apache config file I've got this:
Alias /mywebapp/ "C:/webapps/mywebapp/public_html/"
Which is where my website is. Is there any way of getting that value from php? That way I could just have a line something like:
include($_SERVER["WEBSITE_ALIAS_ROOT"]."/config.inc.php");
(NB. not a genuine server variable - that I know of!) at the top of each file, and it would work on my dev (Windows) and live (Linux) servers without modification.
Does that make sense? I can try and explain it better if I've just made it more confusing!
Thanks
Dave
Alias /mywebapp/ "C:/webapps/mywebapp/public_html/"
To clarify, are you setting the Alias to be exactly the same as the document root? If so, then you are creating a situation where you claim that the path to a document is in two locations at the same time. For example, for example.com/foo.php should foo.php be placed in public_html or mywebapp? It can't be both.
Alias is designed to add a directory outside the document root to be added to the web document tree, but as a subdirectory off the document root, not as a substitution of the document root itself.
[httpd.apache.org...]
So you would have
define [uk.php.net] (ALIAS_PATH, 'path/to/your/alias');
in one file then just include that file at the top of each script. As you can include files from almost any location you could have a single 'config' file and use it with everything.
say you have your site folder with
[www]
[include]
point apache root to the www folder
in your web page, to include a file, simply use
include ("../include/include.inc");
problem solved, this works as i use it all the time... you can't see the includes folder from the outside world, just internally.
in case of multiple folders/subdirs, you dont know how many ../ you need to use to reach your folder and you cant use /path/to/file, since this will point into the root folder (www)
thats why we try to find a replacement of DOCUMENT_ROOT so as to work in an unlimited depth of dir/subdir
To clarify, the DocumentRoot is something like "C:\Apache\htdocs\", and this is what is returned by $_SERVER['DOCUMENT_ROOT'].
The website I'm working on is in a different folder entirely:
Alias /mywebapp/ "C:/webapps/mywebapp/public_html/"
So I access the website using http://localhost/mywebapp/
I did consider setting a variable with this path in it, but it would be different on my dev box to the live server. The idea of defining a variable in a file and then just including that file is good, but has the same problem - how can I include that file in a PHP file from any level?
I'm thinking I might have to just go with the option of putting any number of ../../../ in the include line, depending on where the file is... *sigh*
[edited by: eelixduppy at 12:09 pm (utc) on Nov. 16, 2007]
[edit reason] delinked [/edit]
put in your root directory a file a_file.php
in it: include ("../congif.php");
so this file actually includes a file from below root level
now in all your pages just: include($_SERVER['DOCUMET_ROOT']."/a_file.php");
hope this helps or give you some ideas - i cant test it
(this is a very frustrating problem isn't it!)
I have exactly the same problem, windows dev env & linux site with redirection leading to document-root not giving me what I need.
I've considered pretty-much most of the suggestions above & they are not the ideal solutions, although they will do it.
Have you (or did you) find a way to do this?
thanks,
Rod
<?php
$siteroot = "http://localhost:8080/";
// $siteroot = "http://www.domain.com/";
?>
All links to pages are in the form <a href="<?php print $siteroot;?>directory/directory/file.php">text</a>
When I switch between environments I comment out the addess not required. You could probably come up with something similar which would work for you.
I also use another variable $prefix which is set at the directory level and provides relative addressing for includes and images for pages within that directory.
$prefix = "../../"; (or whatever)
Links are:
<?php require("{$prefix}includes/file.inc");
<img alt="" src="<?php print $prefix;?>images/image.jpg" />
after not getting any joy sorting this out, what I did in the end was this (similar to what you said, Harry). In the root of my site I had a config.inc.php file. I had two different versions of config.inc.php - one my Windows dev environment:
$path ='C:\data\includes';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
and one for my live Linux box:
$path = '/data/home/mydir/includes';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
This added my includes directory to the existing one. When I copied my site live, I made sure I didn't copy config.inc.php.
Then, in every php file the first line I had was
include('config.inc.php');
or
include('../config.inc.php');
or whatever, depending on the directory depth (this was the irritating bit, having to change this line for each file). After that though I could include files without any directory prefix.
Having said all this, I've since changed the way I do it entirely - I now use the marvellous mod_rewrite to redirect all requests to a single index file in the root, and load the content depending on the requested uri.
I hope this helps, although I'm with sonjay - if you really need this functionality, it would be nice if the hosting provider would let you do it the easy way and avoid all these workarounds! :-)
Dave
Just a couple of comments here of things I tried that may help others:
- .htaccess with a SetEnv or a SetEnvIf, then use $_ENV to get at it
- dump $_ENV & see what's there
- check if the ISP sets anything else
(my ISP sets SUBDOMAIN_DOCUMENT_ROOT as a _SERVER variable, and this is what I can use, and emulate it in my dev env)
thanks again guys,
Rod