如何为每个目录设置 PHP 的 auto_prepend_file 指令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4248140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set Php's auto_prepend_file directive per directory?
提问by CLJ
Background: I've got some code that checks to see if a user has a valid session before processing the php page that I would like to set as the auto_prepend_file. However, I need to exclude the page where the user attempts to login from requiring a valid session. I would like to be able to set the auto_prepend_file value on an per directory basis.
背景:我有一些代码可以在处理我想设置为 auto_prepend_file 的 php 页面之前检查用户是否具有有效的会话。但是,我需要将用户尝试登录的页面从要求有效会话中排除。我希望能够在每个目录的基础上设置 auto_prepend_file 值。
Environment: PHP 5.2.6m Apache 2, running php as a cgi not as mod_php, on Windows (if that matters) and on a machine that I have complete control over (not a hosted environment)
环境:PHP 5.2.6m Apache 2,在 Windows(如果这很重要)和我可以完全控制的机器(不是托管环境)上运行 php 作为 cgi 而不是 mod_php
Using a htaccess file is out b/c I am not using mod_php. I have not been able to alter the in php.ini to set the auto_prepend_file, the server throws an internal error. And ini_set() does not work b/c it has already loaded the session checking file before I can change the value of auto_prepend_file.
使用 htaccess 文件是 b/c 我没有使用 mod_php。我无法更改 php.ini 以设置 auto_prepend_file,服务器抛出内部错误。而且 ini_set() 不起作用 b/c 它已经加载了会话检查文件,然后我才能更改 auto_prepend_file 的值。
I do not see a way to set auto_prepend_file on a per directory basis if you are not using mod_php/htaccess. Am I missing something?
如果您不使用 mod_php/htaccess,我看不到在每个目录的基础上设置 auto_prepend_file 的方法。我错过了什么吗?
采纳答案by Matthew
回答by robjmills
You mention you can't use .htaccess files but can you make modifications to httpd.conf and do something like this:
您提到您不能使用 .htaccess 文件,但您可以修改 httpd.conf 并执行以下操作:
<Directory "c:/wamp/www/dev/">
Php_value auto_prepend_file c:/wamp/www/dev/prepend.php
</Directory>
EDIT- just realised this doesnt work when running as CGI. I think though that one thing that will work is if you create a copy of your php.ini file, place it in the directory, and put your prepend directive in there like:
编辑- 刚刚意识到这在作为 CGI 运行时不起作用。我认为,如果您创建 php.ini 文件的副本,将其放在目录中,然后将 prepend 指令放在那里,那么可以使用的一件事是:
auto_prepend_file = c:/wamp/www/dev/prepend.php
回答by édouard Lopez
There is a great tutorial called: Automatically Include Files with PHP & Apachethat explain how to do that with the apache directiveand PHPcode to append at the end. First, define a file to catch the page before it's outputted and append whatever you want:
有一个很棒的教程叫做:Automatically Include Files with PHP & Apache解释了如何使用apache 指令和PHP代码来执行此操作,以追加到最后。首先,定义一个文件以在输出页面之前捕获页面并附加您想要的任何内容:
<?php
{
$script = <<<GA
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxxx-y']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
GA;
$replace = array('</body>','</BODY>');
$page = str_replace($replace, "{$script}\r</body>", $page);
// $replace2 = array('</title>','</TITLE>');
// $page = str_replace($replace2, " - My Awesome Site</title>", $page);
return $page;
}
ob_start("appendToFile");
?>
Then add the Apache directive to your virtual host. You need to prependthe PHP file in order to use the ob_start()
method :
然后将 Apache 指令添加到您的虚拟主机。您需要预先添加PHP 文件才能使用该ob_start()
方法:
<Directory "/path/to/folder">
AddType application/x-httpd-php .html .htm
php_value auto_prepend_file /absolute/path/to/apache-prepend.php
</Directory>