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

空扰寡人 提交于 2020-01-04 03:56:27

问题


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



来源:https://stackoverflow.com/questions/6362691/how-can-i-specify-an-auto-prepend-file-in-my-htaccess-file-without-hardcoding-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!