Forum Moderators: coopster

Message Too Old, No Replies

PHP include directory on Apache user account

Problem with include_path in PHP using document_root

         

fishwebby

9:25 am on Oct 1, 2007 (gmt 0)

10+ Year Member



Hello,

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

omoutop

6:50 pm on Oct 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if i understand correctlly with $_SERVER['DOCUMENT_ROOT'] you get something like path/to/server/alias/public_html/www
or somehitng similar, while you want to use path/to/server/myfolder.

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

fishwebby

7:26 am on Oct 5, 2007 (gmt 0)

10+ Year Member



Hi, thanks for your reply.

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

omoutop

10:02 am on Oct 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



sorry i cant help more since i have never come across soemethig like this, to force myself to find a solution.

Perhaps there is an htaccess way, but my knowledge there is to limited.

But if you get a solution.... please post it here. I am curious on how this can be accomplished.

encyclo

10:21 am on Oct 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld fishwebby. You mention this:

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...]

PHP_Chimp

10:21 am on Oct 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not exactly the smartest solution, but how about using a defined constant for your $_SERVER variable?

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.

FriskUK

10:42 am on Oct 5, 2007 (gmt 0)

10+ Year Member



There is an easier way.

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.

omoutop

11:08 am on Oct 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



this will work if page is in root folder.

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

FriskUK

11:16 am on Oct 5, 2007 (gmt 0)

10+ Year Member



I understand, i have this same problem, but when i'm writing code, i know how many levels deep i am so i don't personally find it a problem :)

fishwebby

12:54 pm on Oct 5, 2007 (gmt 0)

10+ Year Member



encyclo - thanks for the welcome.

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]

PHP_Chimp

1:25 pm on Oct 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For include paths you can dont need to use relative paths, you can use absolute ones. So as long as you know the location just use the absolute path, then you dont need to worry about how many ../../'s you need.

fishwebby

1:43 pm on Oct 5, 2007 (gmt 0)

10+ Year Member



Yes, but that doesn't help me move between dev (on Windows) and live (Linux) where the include directories are in different locations...

omoutop

2:26 pm on Oct 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hm just an idea - you have to test it solo - i cant

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

fishwebby

6:36 pm on Oct 5, 2007 (gmt 0)

10+ Year Member



The principle of that works, yes - I can include a file from below root level, but that's not the problem. It's that the DOCUMENT_ROOT variable does not return where my root is. It returns the document root of the webserver, which on the live server is not a directory I have access to. My root directory is in a completely different place (i.e. not a subdirectory of the main server root). $SERVER("DOCUMENT_ROOT") returns the value of "DocumentRoot" from the Apache httpd.conf file. The root of my web space is defined by an "Alias" value in the httpd.conf file, which is in a different location.

(this is a very frustrating problem isn't it!)

freefd

11:09 am on Nov 16, 2007 (gmt 0)

10+ Year Member



Hi FishWebby,

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

HarryM

11:57 am on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't use DOCUMENT_ROOT (there was a thread here about problems with this). Instead I use an include which is called on all pages. This include just has the following code.

<?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" />

sonjay

12:30 pm on Nov 16, 2007 (gmt 0)

10+ Year Member



I'd say get a hosting provider that lets you define the include path in your .htaccess file. It would do exactly what you need (it's what I do all the time), and it's ridiculous that you can't do that.

fishwebby

1:23 pm on Nov 16, 2007 (gmt 0)

10+ Year Member



Hi everyone,

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

freefd

3:32 pm on Nov 16, 2007 (gmt 0)

10+ Year Member



Well, I've had enough of trying all the various options, so I know my way forward. Thanks for the replies guys, you've made a first-timer want to come back soon ;-)

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