简体   繁体   English

如何在没有硬编码绝对路径的情况下在.htaccess文件中指定auto_prepend_file?

[英]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. 我有一个PHP文件,我想在每个请求的开头执行。 I specified t his in my htaccess file: 我在我的htaccess文件中指定了他:

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. 问题是,该指令的值是在目标脚本的上下文中执行的,而不是在.htaccess文件位置的上下文中执行的。 So it works if the script I'm executing is in the same directory as the prepend file, but otherwise not. 因此,如果我正在执行的脚本与prepend文件位于同一目录中,则无效。

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? 我可以在.htaccess中访问任何变量来在执行时生成绝对路径吗?

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. 您可以创建自动前置文件以使用$_SERVER['DOCUMENT_ROOT'] var来确定使用switch / if-else构造包含哪个文件。

Or you could use the __FILE__ var in combination with realpath to include/require the "prepend" file you want. 或者您可以将__FILE__ var与realpath结合使用以包含/要求所需的“prepend”文件。

auto-prepend.php 自动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. 再想一想, __FILE__可能会引用前置文件而不是请求的文件,因此它可能无法按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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