 |
| Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 13th, 2004, 12:45 PM
|
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
an include question (simple i think)
Howdy php folks,
I'd like to include a 'site configuration' file on every page of my site, and be able to use the same include call, no matter where the file calling the config file is (ie, in the webroot, or several directories deep) - essentially, I'd like to include this file with a relative path, but
Code:
<? include("/includes/config.php"); ?>
doesn't work for including relative to the webroot.
Right now I supply the entire local path to the file:
Code:
<? include("/Users/darinsee/Sites/contenttest/includes/config.php"); ?>
Is there any way that I can include this file in a more flexible, generic way? Something like:
Code:
<? include($SOME_VAR_THAT_GETS_THE_WEBROOT."/includes/config.php"); ?>
Thanks for considering this question!!!
Darin
|
|

December 13th, 2004, 01:16 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
I think you're trying to get a directory name because it's the root of your site, even if it is not the root of your filesystem. However, how would PHP know that your site resides in that folder, if PHP runs system-wide?
An easy solution is to define the path to your site in a short variable at the beginning of your file, and use that variable for your include path.
Code:
$p = "/Users/darinsee/Sites/contenttest";
include($p."/includes/config.php");
-Snib - http://www.snibworks.com
Where will you be in 100 years?
|
|

December 13th, 2004, 02:29 PM
|
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Howdy Snib and thanks for the quick response!
I guess I want to avoid having to define (in your example) $p in every page of my site - but maybe I won't be able to do that with php... or at least just php alone.
In asp, when including, you can use
(includes relative to the webroot - it doesn't seem like this can be done in php)
(includes relative to the file calling the include in the filesystem - this works in php)
I'd like to somehow emulate the asp 'virtual' method of including a file, so that the same reference can be used on any page of the site, and things like the path defined in your variable, $p can be left for definition in the config.php file itself.
Kind of a problem of chicken before egg... but there must be some crafty way to do it... maybe javascript? I'm looking into that right now...
Thanks for any help you can give on this!
Darin
|
|

December 13th, 2004, 02:44 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
If you could actually run SSI in this situation, this:
...would take you back to /Users/blah blah anyway. This problem doesn't have to do with PHP or ASP or SSI, but with the way you're running your site.
Basically what the problem is, is that whenever you refer to a file with a / in front of the URL, it goes all the way back to the root of the filesystem, as far as it can. You need to define the "contenttest" folder as the root, possibly by running a web server such as Apache.
It's pretty complicated, but I hope you grasp my explanation. :-)
-Snib - http://www.snibworks.com
Where will you be in 100 years?
|
|

December 13th, 2004, 03:34 PM
|
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Howdy Snib - thanks for taking another shot at this -
I guess I understand - I've just gotten used to putting a slash in front of a directory or file when i wanted the link to say "look for this file starting at the webroot" - and it's always worked for me with SSI and ASP, without making another changes to Apache or IIS - (I'm running apache with PHP).
So there's no variable or constant in php that looks for the webroot in which the file calling the variable or constant resides?
I found a link that kind of describes the trouble I'm having: http://www.hardcoder.com/scripting/p...lude_files.php
Thanks Snib,
Darin
|
|

December 13th, 2004, 09:58 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Briefly:
PHP does not know what the web root is. To PHP, "contenttest" is just another folder in the filesystem of the disk you're running it on.
The easiest way I can think of is to do what I said in my first post..
hth
-Snib - http://www.snibworks.com
Where will you be in 100 years?
|
|

December 14th, 2004, 03:25 PM
|
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks again snib..
Darin
Quote:
quote:Originally posted by Snib
Briefly:
PHP does not know what the web root is. To PHP, "contenttest" is just another folder in the filesystem of the disk you're running it on.
The easiest way I can think of is to do what I said in my first post..
hth
-Snib - http://www.snibworks.com
Where will you be in 100 years?
|
|
|

December 15th, 2004, 01:57 AM
|
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi All - Just thought I'd post a link to the place where I found out what Snib was talking about (I think).
Anyhow it solved my problem (how to use relative includes in php) - I needed to set up a virtualhost in Apache pointing to the directory of the site that I was trying to do relative includes from. Once I did this, I was able to use an absolute path like this:
Code:
<? include($_SERVER['DOCUMENT_ROOT']."/includes/site_config.php"); ?>
from anywhere in the site's directory structure.
For those working on Mac OS X machines (and need to set up a php development environment similar to your live hosted webservers), this link will make you very happy in terms of getting virtual hosts set up:
http://www.evolt.org/MacOSX_vhosts/
Best,
Darin
Quote:
quote:Originally posted by darinsee
Thanks again snib..
Darin
Quote:
quote:Originally posted by Snib
Briefly:
PHP does not know what the web root is. To PHP, "contenttest" is just another folder in the filesystem of the disk you're running it on.
The easiest way I can think of is to do what I said in my first post..
hth
-Snib - http://www.snibworks.com
Where will you be in 100 years?
|
|
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| simple question |
petergoodman |
XSLT |
8 |
July 18th, 2008 07:57 AM |
| very simple question |
rogdawg |
Visual Studio 2005 |
2 |
January 14th, 2008 04:03 PM |
| Simple Question |
dpkbahuguna |
Java Basics |
2 |
May 19th, 2006 12:05 AM |
| Simple Question |
JEHalm |
BOOK: Beginning Access VBA |
0 |
December 29th, 2005 10:42 AM |
|
 |