aw crud
aw crud

Reputation: 8891

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?

Upvotes: 3

Views: 5241

Answers (1)

gnur
gnur

Reputation: 4733

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.

Upvotes: 3

Related Questions