environmental variables...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
arpowers
Forum Commoner
Posts: 76
Joined: Sun Oct 14, 2007 10:05 pm
Location: san diego, ca

environmental variables...

Post by arpowers »

hey guys,
I would like to set a default path for my "php root" constant, that will either load by default into my $_SERVER global or simply just load into my php scripts before anything else.

This way I won't have to worry about relative paths and can link to everything absolutely...

for example rather than using:
include('/includes/file.php')

I can use include(ABSPATH.'includes/file.php');


Thanks
Andrew
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I think your talking about the document root and not php's root directory. If you want this value automatically loaded in all your scripts you could pop the following code in a auto-prepended file by apache, if not simply include a file that defines the path.

Code: Select all

define('ABSPATH', $_SERVER['DOCUMENT_ROOT']);
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

If it's going to be used in include, dirname(__FILE__) is usually what you want.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

If you're using Apache you can define it in an .htaccess file ... for example ...

Code: Select all

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule .* - [E=mypath:/path/to/site/]
RewriteRule ^page\/([0-9]+)\/(.*)$ %{ENV:mypath}page.php?id=$1&%1 [NC,L]
That will let you access pages using http://www.domain.com/page/123/page-tit ... -here.html ... the page requested will be page.php?id=123 .. and in that page you can use $_SERVER['mypath'] to get "/path/to/site/".
Post Reply