Sign in to follow this  

[web] Trying to reconfigure my site's root

This topic is 4511 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm running Apache on a shared hosting server, along with Perl and PHP. Since it's shared, I don't have access to the global configuration files of these services. Right now, my site is located at: /home/.servername/username/mysite.com/index.html There are certain parts of my site that will remain constant across a large subset of pages, like my header and footer. I'd like to place these into each page by using a PHP include directive, like: <? include("/templates/banner.php") ?> The only problem here is that "/" is interpreted to be the server root "/", not the site root "/home/.servername/username/mysite.com", as I'd prefer. Some people have suggested using PHP's $DOCUMENT_ROOT environment variable, but that seems like a kludge. I'd really prefer to tell Apache directly to rewrite my site root accordingly. Is there anything I can do, or do I need to resort to a hackish solution? Any help would be greatly appreciated. Thanks!

Share this post


Link to post
Share on other sites
I'm not entirely sure, but I think it's somewhere in php.ini. I would recommend staying away from it though as it will bring up millions of conflicts if you try to run the script on someone else's server. I'd go with sticking to $_SERVER["DOCUMENT_ROOT"] and $_SERVER['PHP_SELF'].

Share this post


Link to post
Share on other sites
If you are able to, you may be able to set PHP's include_path to include your site root. This is ok if you have the ability to set php_value in the Apache config, but not ok otherwise.

Another option is to set this dynamically. include_path can be set at any time in the script, so you can manually add $_SERVER['DOCUMENT_ROOT'] to it.

The bad news is that Windows and non-Windows systems use different path separator characters which will have to be determined at runtime (or placed in a machine-specific config file).

The other bad news is that if you're going to set include_path at runtime, you'll have to include whatever file does that before it's set (chicken & egg).

BUT you can always use relative includes. But remember they're relative to the currently executing PHP file.

Relative includes are always preferable. So personally I'd say

1. Put a relative include of some common base setup file at the top of each script (require('includes/setup.php') or some such thing like require('../../includes/setup.php') if several levels deep)
2. In setup.php, change the include_path to add $_SERVER['DOCUMENT_ROOT']
3. You can now include everything else with

require('includes/blah.php');


Even if the page which includes it is somewhere completely different.

Moreover, the app will be entirely independent of which directory it is running in, which saves major deployment headaches.

There's nothing worse than having two copies of an application on a box (Say staging and production versions), and discovering that they're accidentally sharing files (i.e. if that was not intended)

Mark

Share this post


Link to post
Share on other sites
Quote:
Original post by kSquared
<? include("/templates/banner.php") ?>


Have you tried it without the inital "/", i.e.

<? include("templates/banner.php"); ?>

EDIT: spelling

[Edited by - MENTAL on August 7, 2005 9:16:41 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by MENTAL
Quote:
Original post by kSquared
<? include("/templates/banner.php") ?>


Have you tried with without the inital "/", i.e.

<? include("templates/banner.php"); ?>


That works for me.

Share this post


Link to post
Share on other sites

This topic is 4511 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this