php virtual,PHP: virtual - Manual

这篇博客介绍了如何将PHP安装为Apache模块,以实现自定义的预处理器和信息日志功能。通过配置,所有以'pre.php'开头的请求首先会由'pre.php'处理,然后返回给用户。内容涉及到路径解析、文件操作、内容处理以及使用`virtual`函数进行内容修改和交付。这种方法可以扩展用于添加水印、转换为XML、检查文件扩展名、代理内容等。
摘要由CSDN通过智能技术生成

when php is installed as an apache module, this works pretty well for writing your own php preprocessor/information logger. For example, requests to any URI underneath pre.php will first be executed by pre.php, then returned to the user.

$docroot        = $_SERVER['DOCUMENT_ROOT'];

$script_root    = str_replace( basename($_SERVER['SCRIPT_NAME']),'',$_SERVER['SCRIPT_NAME'] );

$script_ext     = substr( $_SERVER['SCRIPT_NAME'], strrpos( $_SERVER['SCRIPT_NAME'],'.' ) );

$fakework_root  = $script_root.basename( $_SERVER['SCRIPT_NAME'] ).'/';

$framework_root = $script_root.'_'.basename( $_SERVER['SCRIPT_NAME'], $script_ext ).'/';

$frequest_root  = dirname( $framework_root.substr( $_SERVER['PATH_INFO'], 1 )).'/';

$frequest_name  = basename( $_SERVER['PATH_INFO'] );

$frequest_ext   = (strrpos($frequest_name,'.')===FALSE ? FALSE : strtolower(substr( $frequest_name, ( strrpos( $frequest_name, '.' )+1 ) ) ) );

$frequest_full  = $frequest_root.$frequest_name;

$doc_frequest   = $docroot.$frequest_full;

$doc_framework   = $docroot.$framework_root;

$DO_PARSE = in_array( $frequest_ext, $chk_exts );

if( $DO_PARSE )

{

$tmpfname = tempnam( $doc_framework.'tmp', 'aj_' ).($frequest_ext? ('.'.$frequest_ext) : '');

if( ($to_parse=@file_get_contents($doc_frequest))===FALSE )

$to_parse="404";

$tmpvname = str_replace( $docroot, '', $tmpfname );

$tmpvname = str_replace( '\\\\', '/', $tmpvname );

// - - - - - - - - - - - - - - - - - - - - - - - - - - -

//  Do processing of data stored in $to_parse

// - - - - - - - - - - - - - - - - - - - - - - - - - - -

$to_parse = striptags( $to_parse );

// - - - - - - - - - - - - - - - - - - - - - - - - - - -

$handle = fopen($tmpfname, "w");

fwrite($handle, $to_parse);

fclose($handle);

@virtual( $tmpvname.$getvars );

unlink( $tmpfname );

}

else

@virtual( $frequest_full.$getvars );

?>

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值