IV. Apache 特有函数

简介

本类函数仅在 PHP 作为 Apache 的模块运行时可用。

注: 自 PHP 4.3.2 起,Apache 2 SAPI 中不再像和 Apache 1 中相反那样隐含设定 PATH_TRANSLATED,它将被设为 SCRIPT_FILENAME 服务器环境变量而不是由 Apache 产生。此修改是为了符合 CGI 规范,PATH_TRANSLATED 应仅在定义了 PATH_INFO 之时才有效。

Apache 2 用户可以在 httpd.conf 中使用 AcceptPathInfo = On 来定义 PATH_INFO

安装

PHP 在 Apache 中的安装见安装一章。

运行时配置

Apache 的 PHP 模块的行为受 php.ini 的设置影响。在 php.ini 中的设置可以被服务器配置文件或本地的 .htaccess 文件中的 php_flag 设置所覆盖。

例子 1. 用 .htaccess 禁用一个目录的 PHP 解析

php_flag engine off

表格 1. Apache 配置选项

名称默认值可修改范围更新记录
engine"1"PHP_INI_ALL自 PHP 4.0.5 起可用
child_terminate"0"PHP_INI_ALL自 PHP 4.0.5 起可用
last_modified"0"PHP_INI_ALL自 PHP 4.0.5 起可用
xbithack"0"PHP_INI_ALL自 PHP 4.0.5 起可用
有关 PHP_INI_* 常量进一步的细节与定义参见附录 G

以下是配置选项的简要解释。

engine boolean

打开或关闭 PHP 解析。本指令仅在使用 PHP 的 Apache 模块版本时才有用。可以基于目录或者虚拟主机来打开或者关闭 PHP。将 engine off 放到 httpd.conf 文件中适当的位置就可以激活或禁用 PHP。

child_terminate boolean

指定 PHP 脚本在请求结束后是否可以要求终止子进程。参见 apache_child_terminate()

last_modified boolean

在本次请求中发送一个头信息 Last-Modified:,显示 PHP 脚本最后被修改的日期。

xbithack boolean

不管文件结尾是什么,将文件作为 PHP 以可执行位组来解析。

资源类型

本扩展模块未定义任何资源类型。

预定义常量

本扩展模块未定义任何常量。

目录
apache_child_terminate -- 在本次请求结束后终止 apache 进程
apache_get_modules -- Get a list of loaded Apache modules
apache_get_version -- Fetch Apache version
apache_getenv -- Get an Apache subprocess_env variable
apache_lookup_uri -- 对指定的 URI 执行部分请求并返回所有有关信息
apache_note -- 取得或设置 apache 请求记录
apache_request_headers -- Fetch all HTTP request headers
apache_reset_timeout -- Reset the Apache write timer
apache_response_headers -- Fetch all HTTP response headers
apache_setenv -- Set an Apache subprocess_env variable
ascii2ebcdic -- Translate string from ASCII to EBCDIC
ebcdic2ascii -- Translate string from EBCDIC to ASCII
getallheaders -- Fetch all HTTP request headers
virtual -- Perform an Apache sub-request

add a note add a note User Contributed Notes
bgshea at gmail dot com
29-Nov-2005 11:41
here is a dynamic version of henk_nicolai at REMOVE-THIS at hotmail dot com's code

   $req = $_SERVER['REQUEST_URI'];
   // Remove rubbish.
   $newReq = ereg_replace ( $_SERVER['SCRIPT_NAME'] . '[^?]*', $_SERVER['SCRIPT_NAME'], $req);
   if (strlen($newReq) < strlen($req))
   {
       header ('Location: '.$newReq);
       header ('HTTP/1.0 301 Moved Permanently');
       die;  // Don't send any more output.
   }
   unset($req);
   unset($newReq);

this can be placed at the top of any file that is to be access by the URI.
pike
02-Nov-2005 07:16
to henk_nicolai

the behaviour you describe is not a "glitch" of apache :-). an url like
"http://my_server.nl/index.php/foo".  should return the resource http://my_server.nl/index.php and pass "/foo" as PATH_INFO in the environment.

which is extremely usefull if you use it wisely.

for more info on PATH_INFO and PATH_TRANSLATED, see http://nl2.php.net/reserved.variables . PATH_INFO is not related to the php pathinfo() function

$2c,
*pike
outofnet at mail dot ru
27-Aug-2004 10:44
Important info for Apache2 users that have several virtual hosts.

It seems php_flag directive has a different behaviour under Apache 2 (from what it is under 1.3) when used inside <VirtualHost> block.

If you override global php.ini settings with php_flag for one of your virtual host - then your other non-customized virtual hosts may use this overrided settings as well. php_flag records are messed up among different virtual hosts under single Apache 2 server. It may result from Apache 2 multi-thread nature.

Here is an example:

Suppose you have two Virtual hosts: V1 and V2.
For V1 in Apache configuration you use
php_flag magic_quotes_gpc 1
V2 is supposed to use global php.ini settings, so you didn't put any php_flag records into Apache conf for V2 (this worked under Apache 1.3).
And your default php.ini settings are:
php_flag magic_quotes_gpc 0

When you run your server you'll notice that magic quotes is (sometimes) set to On at V2!
The value turns On at V2 when there have been a previous request to V1.

To solve the problem either move php_flag into .htaccess located inside customized virtual host directory OR put php_flag with default settings into all your <VirtualHost> blocks that are not customized. So for V2 put:
php_flag magic_quotes_gpc 0

It is critical to be very carefull with php_flag engine 0.

My configuration is:
PHP 4.3.4, Apache 2.0.50, Linux RedHat 9
henk_nicolai at REMOVE-THIS at hotmail dot com
20-Nov-2002 08:03
My Apache server has a problem when someone enters a URI like: "http://my_server.nl/index.php/". (Note the extra slash.) The server executes the index.php script anyway, which causes the browser directory and the current directory used in the script to be different. And therefore my relative links don't work, and my stylesheet is not loaded. A quick test ("http://www.php.net/manual/en/index.php/") reveals that also this site has this glitch.

When a client requests a directory without the last slash ("http://www.php.net/manual") the server sends a HTTP 301 (Moved Permanently) response with a redirect to the correct URI ("http://www.php.net/manual/"), and my idea was to do the same when the user adds a slash too much:

<?php
   $req
= $_SERVER['REQUEST_URI'];
  
// Remove rubbish.
  
$newReq = ereg_replace ('index.php[^?]*', 'index.php', $req);
   if (
strlen($newReq) < strlen($req)) {
    
header ('Location: '.$newReq);
    
header ('HTTP/1.0 301 Moved Permanently');
     die; 
// Don't send any more output.
  
}
   unset(
$req); unset($newReq);

   ... (
rest of the script) ...
?>

Replace every occurence of 'index.php' with your filename and you're done. Hope it helps. :-)

(Note: I'm not using fragments in my URI's (like 'index.php#bottom'), and this code may not do what you want if you are using them.)
cjm2 at earthling dot net
11-Jan-2002 07:40
If you are trying to find a Handler to use with apache's mod_mime functions (e.g. SetHandler).  Use the MIME type associated with php.

e.g. SetHandler application/x-httpd-php
jarl at diku dot dk
25-Mar-2000 07:12
Many of the environment variables can be found here:
http://www.php.net/manual/language.variables.predefined.php