我怎么可以指定一个AUTO_ prepend_file在我的.htaccess文件中没有硬编码的绝对路径? [英] How can I specify an auto_prepend_file in my .htaccess file without hardcoding the absolute path?

查看:524
本文介绍了我怎么可以指定一个AUTO_ prepend_file在我的.htaccess文件中没有硬编码的绝对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要在每个请求的开始执行PHP文件。我指定的T他在我​​的htaccess文件:

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"

现在的问题是,这个指令的值在目标脚本的上下文中.htaccess文件位置的上下文中执行,而不是。因此,它的工作原理,如果我执行脚本是在相同的目录prePEND文件,但在其他方面没有。

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.

我知道我可以使用绝对路径,但不是很便携。有没有什么变化,我可以在.htaccess访问生成在执行时的绝对路径?

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?

推荐答案

简短的回答,没有。

多头回答,你也许可以让它工作像你想。

Longs answer, you can probably make it work like you want to.

您可以创建自prePEND文件使用 $ _ SERVER ['DOCUMENT_ROOT'] VAR来确定使用要包括的文件中的开关/的if-else constructin。

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.

或者您可以使用 __ FILE __ VAR结合真实路径来包括/要求prePEND 文件,你想要的。

Or you could use the __FILE__ var in combination with realpath to include/require the "prepend" file you want.

自动prepend.php
require_once(真实路径(__ FILE__)/$p$ppend.php');

auto-prepend.php
require_once(realpath(__FILE__) . '/prepend.php');

//编辑:
想到这一些, __ FILE __ 可能是指在所需的文件代替了prePEND文件,因此如预期可能无法正常工作。

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

这篇关于我怎么可以指定一个AUTO_ prepend_file在我的.htaccess文件中没有硬编码的绝对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆