问题
I have a PHP file I want to execute at the beginning of each request. I specified t his in my htaccess file:
php_value auto_prepend_file "alwaysrunthis.php"
The problem is, the value of this directive is executed in the context of the target script, and not in the context of the .htaccess file location. So it works if the script I'm executing is in the same directory as the prepend file, but otherwise not.
I realize I can use absolute path but that is not very portable. Is there any variable I can access in .htaccess to generate the absolute path at execution time?
回答1:
Short answer, no.
Longs answer, you can probably make it work like you want to.
You could create the auto prepend file to use the $_SERVER['DOCUMENT_ROOT'] var to determine which file to include using a switch/if-else constructin.
Or you could use the __FILE__ var in combination with realpath to include/require the "prepend" file you want.
auto-prepend.phprequire_once(realpath(__FILE__) . '/prepend.php');
//edit:
thought of it some more, __FILE__ will probably refer to the prepend file in stead of the requested file, so it might not work as expected.
来源:https://stackoverflow.com/questions/6362691/how-can-i-specify-an-auto-prepend-file-in-my-htaccess-file-without-hardcoding-t