简体   繁体   English

什么会导致PHP变量被服务器重写?

[英]What would cause PHP variables to be rewritten by the server?

I was given a VM at my company to install web software on. 我在公司获得了一台虚拟机来安装网络软件。 But I came across a rather bizarre issue where PHP variables would be overwritten (rewritten) by the server if they matched a specific pattern. 但是我遇到了一个相当离奇的问题,如果PHP变量与特定模式匹配,它们将被服务器覆盖(重写)。 What could rewrite PHP variables like this? 什么可以像这样重写PHP变量?

The following is as an entire standalone script. 以下是整个独立脚本。

<?php
$foo = 'b.domain.com';
echo $foo; // 'dev01.sandbox.b.domain.com'

$bar = 'dev01.sandbox.domain.com';
echo $bar; // 'dev01.sandbox.sandbox.domain.com'

$var = 'b.domainfoo.com';
echo $var; // 'b.domainfoo.com' (not overwritten because it didn't match whatever RegEx has been set)
?>

Essentially any variable which contains a subdomain and matches on the domain name would be rewritten. 基本上任何包含子域和域名匹配的变量都将被重写。 This isn't something mod_rewrite would be able to touch, so it has to be something at the server level that is parsing out PHP and rewriting a string if it matches a RegEx. 这不是mod_rewrite能够触及的东西,所以它必须是服务器级别的东西,它解析出PHP并重写一个字符串,如果它匹配RegEx。

Output overwriting is possible within Apache by using mod_perl: PerlOutputFilterHandler. 使用mod_perl:PerlOutputFilterHandler可以在Apache中进行输出覆盖。

The following could be added to an apache.conf to set the output filter: 可以将以下内容添加到apache.conf中以设置输出过滤器:

<FilesMatch "\.(html?|php|xml|css)$">
    PerlSetVar Filter On
    PerlHandler MyApache2::FilterDomain
    PerlOutputFilterHandler MyApache2::FilterDomain
</FilesMatch>

Example filter handler code: 示例过滤处理程序代码

#file:MyApache2/FilterDomain.pm
#--------------------------------
package MyApache2::FilterDomain;

use strict;
use warnings;

use Apache2::Filter();
use Apache2::RequestRec();
use APR::Table();

use Apache2::Const -compile => qw(OK);

use constant BUFF_LEN => 1024;

sub handler {
    my $f = shift;
    my @hostname = split(/\./, $f->r->hostname);
    my $new_hostname = $hostname[0].".".$hostname[1];

    unless ($f->ctx) {
        $f->r->headers_out->unset('Content-Length');
        $f->ctx(1);
    }

    while ($f->read(my $buffer, BUFF_LEN)) {
        $buffer =~ s/([a-z0-9]+)+\.domain\./$new_hostname\.$1.domain\./g;   
        $f->print($buffer);
    }

    return Apache2::Const::OK;
}
1;

More on Apache mod_perl filters can be found here: mod_perl: Input and Output Filters 有关Apache mod_perl过滤器的更多信息,请参见: mod_perl:输入和输出过滤器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM