View Full Version : Absolute Paths & Virtual Hosts
jkies00
05-24-2006, 01:41 AM
Hi All,
When I built my first PHP website, I needed a way to include files from anywhere in my file structure. This code worked perfectly:
<?php include($_SERVER["DOCUMENT_ROOT"].'/_includes/components/header.php'); ?>
Now I have multiple sites that I run locally under virtual hosts in Apache. For my includes to work now, I need the path to resolve to the virtual host directory, instead of the localhost htdocs directory.
How can I get the include path to resolve to the virtual host directory?
Please help!
lavinpj1
05-24-2006, 03:38 AM
If a page is running under a virtual host, then surely the root of the host is /
~Phil~
jkies00
05-24-2006, 11:31 AM
/ isn't working either. When I get the real path of '/',
echo realpath('/');
It returns "C:\"...
There must be a way to do this... :o(
d11wtq
05-25-2006, 12:00 AM
The usual way to do this is to use a config file that defines the root of the application.
Depending upon how your files are orgainised you might be able to work around it by using getcwd() or dirname(__FILE__) along with the logic to suss it out ;)
jkies00
05-26-2006, 05:07 PM
That sounds like what I need, but I don't really understand. Do you know of a thread or a website that explains it??
JamieD
05-27-2006, 12:47 AM
You should be able to create an .htaccess file in your root web folder (the location of your index page) in this file add following the line:
php_value include_path ".:/path/to/include/folder"
you can then put all of your php include files in this directory and simple include them using only their file name, e.g.
include('my_include_file.php');
You could even include more that one directory in this path if you want to split your include files up.
php_value include_path ".:/path/to/include/folder:/path/to/another/include/folder"
If you are using windows you will need to use the following syntax instead:
php_value include_path = ".;C:\path\to\include\folder;C:\path\to\another\include\folder"
HTH
Jamie
jkies00
05-31-2006, 01:46 PM
That seems like a reasonable approach, but is there a cross-OS way to do it? I develop on a WinXP machine, but my hosting is Linux.
Ideally, I'd like a solution that wouldn't need to be changed when I upload it. So the requirements would be:
Include files from anywhere in a directory structure using the same code
Paths to includes should be relative to the document root of the virtual host in which it exists
Must work the same regardless of Operating System
Please feel free to correct my vocabulary, as I'm still new to php.
JamieD
06-01-2006, 09:59 AM
Include files from anywhere in a directory structure using the same code
Paths to includes should be relative to the document root of the virtual host in which it exists
Must work the same regardless of Operating System
There is no cross platform way to do this, windows uses \ and linux uses / in file paths. However, you only need a single .htaccess file in the root of your website and this will apply to your entire website. Its not a big deal to have two different .htaccess files as once you have set it up you will not have to edit it again.
I would suggest setting up your php include directory outside of your public web folder, this is a more secure way to have included PHP files, especially if you have passwords in your php files. Once you have setup your .htaccess fiiles you can use the same code on both windows and linux systems no matter where your php files are.
Jamie
jkies00
06-05-2006, 02:13 AM
Are you aware of a tutorial that explains how to set up htaccess?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.