简体   繁体   中英

How can I specify an auto_prepend_file in my .htaccess file without hardcoding the absolute path?

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?

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.php
require_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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM