Page 1 of 1

environmental variables...

Posted: Tue Dec 25, 2007 6:44 pm
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

Posted: Tue Dec 25, 2007 10:48 pm
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']);

Posted: Thu Dec 27, 2007 6:21 pm
by Ambush Commander
If it's going to be used in include, dirname(__FILE__) is usually what you want.

Posted: Fri Dec 28, 2007 3:19 am
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/".