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

发布于 2025-01-05 23:06:31 字数 533 浏览 0 评论 0原文

我的公司给了我一个虚拟机来安装网络软件。但我遇到了一个相当奇怪的问题,如果 PHP 变量与特定模式匹配,服务器就会覆盖(重写)它们。什么可以像这样重写 PHP 变量?

以下是一个完整的独立脚本。

<?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)
?>

本质上,任何包含子域并与域名匹配的变量都将被重写。这不是 mod_rewrite 能够触及的东西,所以它必须是服务器级别的东西,解析 PHP 并重写字符串(如果它与正则表达式匹配)。

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. What could rewrite PHP variables like this?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞花火° 2025-01-12 23:06:31

在 Apache 中可以使用 mod_perl: PerlOutputFilterHandler 进行输出覆盖。

可以将以下内容添加到 apache.conf 中以设置输出过滤器:

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

示例过滤器处理程序代码:

#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;

有关 Apache mod_perl 过滤器的更多信息可以在此处找到: mod_perl:输入和输出过滤器

Output overwriting is possible within Apache by using mod_perl: PerlOutputFilterHandler.

The following could be added to an apache.conf to set the output filter:

<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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文