简体   繁体   中英

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 . Most likely you need this value to construct a URI to the script. 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 .

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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