简体   繁体   English

在PHP中,$ _ SERVER ['HTTP_HOST']的Perl等价于什么?

[英]What is the Perl equivalent of $_SERVER['HTTP_HOST'] in PHP?

如何在Perl脚本中获取当前域名,即$_SERVER['HTTP_HOST'] PHP变量的等效Perl代码?

The hostname alone is almost never useful, you also want the server port, in combination nicknamed netloc . 单独的主机名几乎永远不会有用,您还需要服务器端口(昵称为netloc) Most likely you need this value to construct a URI to the script. 您很可能需要此值来构造脚本的URI。 This is already included in the frameworks, no need to do this manually. 这已经包含在框架中,无需手动进行。 Code samples for both ways following. 以下两种方式的代码示例。


In PSGI, read the "variable" HTTP_HOST (alternatively, SERVER_NAME and SERVER_PORT ) from the PSGI environment hash, or call the uri method in Plack::Request . 在PSGI中,从PSGI环境哈希中读取“变量” HTTP_HOST (或者, SERVER_NAMESERVER_PORT ),或在Plack :: Request中调用uri方法

use Plack::Request qw();
my $app = sub {
    my ($env) = @_;
    my $req = Plack::Request->new($env);
    return [200, ['Content-Type' => 'text/plain'], [
        sprintf "Host: %s\nURI: %s", $env->{HTTP_HOST}, $req->uri
    ]];
};

In CGI, combine the POSIX environment variables SERVER_NAME and SERVER_PORT , or call the url method. 在CGI中,组合POSIX环境变量SERVER_NAMESERVER_PORT ,或调用url方法。

use CGI qw();
my $c = CGI->new;
print $c->header('text/plain');
print "Host: $ENV{SERVER_NAME}:$ENV{SERVER_PORT}\n";
print "URI: " . $c->url;

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

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