I would like to prepend a PHP script automatically using .htaccess
. I know that there is a directive to do this. I have the following in my .htaccess
file in the web root directory:
php_value auto_prepend_file "./init.php"
The contents of the init.php
file include:
<?php
$_SERVER['WEB_ROOT']=
str_replace($_SERVER['SCRIPT_NAME'],'',
$_SERVER['SCRIPT_FILENAME']);
This calculates the path of the web root and puts it into the superglobal. It is the only practical way I can think of keep track of the web root for things like includes.
The problem is that this only works for for PHP scripts also in the web root. If the script itself is in a sub directory I get an error, because the init.php
file is not in the current directory. I don’t want to specify an absolute path to init.php
because the current is a bit volatile at the moment.
I can think of 2 solutions:
- include a
.htaccess
file in the sub directories pointing to the parent directory using a relative path (../init.php
) - include a
init.php
file in all of the subdirectories
Is there another way to specify a prepended file which works for all subdirectories?
Failing that, is there another way to put the web root into a superglobal without this nonsense?