virtual() - Apache函数
virtual()
(PHP 4, PHP 5, PHP 7)
执行 Apache 子请求
说明
virtual(string $filename): boolvirtual()是一个 Apache 特有函数,类似于mod_include中的。它执行一个 Apache 子请求。可用于包含一个 CGI 脚本或.shtml文件,或任何其它可通过 Apache 解析的请求。注意对一个 CGI 脚本,该脚本生成合法的 CGI 头,至少必须生成Content-Type头。
为运行子请求,所有缓冲将中止并刷新至浏览器,包括头信息。
、仅在将 PHP 安装为 Apache 模块时,支持此函数。
参数
$filenamevirtual命令将执行的文件
返回值
成功执行 virtual 命令,或失败时返回FALSE
。
更新日志
版本 | 说明 |
---|---|
4.3.3 | 从PHP 4.3.3 起,也可在 Netscape/iPlanet/SunONE Web 服务器的NSAPI 服务器模块使用此函数。 |
4.0.6 | 此函数可用于 PHP 文件。但是通常使用include或require更好。 |
范例
示例请看apache_note()。
注释
Warning查询字符串可被传递至被包含文件,但是$_GET是拷贝于父文件,仅有$_SERVER['QUERY_STRING']将填充传递入的查询字符串。且此查询字符串只在使用 Apache 2 时被填充。此请求文件将不会显示在 Apache 访问日志中。
Note:在被请求文件中设置的环境变量在原请求文件中不可见。
参见
apache_note()
取得或设置 apache 请求记录
Problem with most of the scripts posted below is that virtual() flushes the pending headers before making the subrequest. Requesting an image with virtual() still returns a text/html type document. A workaround is to set the content-type first. But that requires getting the content-type first. I'm using the following script for now. A disadvantage is that Apache makes 2 subrequests.
I have a header that's include()'ed on each of my pages. I then wanted to add a Perl script to that header file (header.php) via the virtual() command. Since my header is used by documents in my /www folder along with other folders inside that (and inside those), and virtual() seems to take only relative paths, I had to write some code to dynamically get the path to the perl script. Hope this helps some ppl out: $cwd = getcwd(); $script_name = "cgi-bin/perl_script.pl"; $count = substr_count($cwd, '/'); $count = $count - 3; // get rid of extra absolute paths since my directory is /home/user/www // Add additional path information for($i = 1; $i So all files in http://server/sub/pre.php/path/ are really located in http://server/sub/_pre/path/ All this needs is some kind of caching mechanism. But yeah, this could be modified to add watermarks with the image functions, convert to xml with Tidy, check for extensions better with mimeTypes, proxy content with cURL, validate $_SERVER['HTTP_REFERER'] or $_SERVER['HTTP_USER_AGENT'], etc etc This gives you much more over than the auto_prepend_file, and auto_append_file, for certain functionality The key is the virtual function _because_ it delivers the modified content with an apache subrequest.
I saw the note above about the length of the query string... but didn't know what it was, so have altered the code so it can post to the script. Probly only works on nix systems as it makes use of the echo function... This code also will look evaluate the result, so you can get cgi to dynamically create PHP (probly best to watch out that posted variables do not include script!)
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!