使用mod_perl提取REMOTE_ADDR

时间:2014-05-02 18:26:09

标签: perl mod-perl

请使用mod_perl帮助如何提取REMOTE_ADDR值。

我有以下perl脚本:

use Apache2::RequestUtil;
use Apache2::RequestRec;

$| = 1;
print "Content-type: text/plain\n\n";

my $r = Apache2::RequestUtil->request;
print $r->as_string();

脚本的结果:

GET /tmp/recheaders.pl HTTP/1.0
X-Scheme: http
REMOTE_ADDR: 81.204.25.44
Host: dom.net
X-Real-IP: 81.204.25.44
X-Forwarded-for: 81.204.25.44
Connection: close
User-Agent: Opera/9.80 (X11; Linux x86_64) Presto/2.12.388 Version/12.16
Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
Accept-Language: en-US,en;q=0.9
Accept-Encoding: gzip, deflate
Cookie: SESSION_ID=963bd96302cd70047c9f017640c7cbb8;
Cache-Control: no-cache

HTTP/1.0 (null)

如何仅提取REMOTE_ADDR: 81.204.25.44?什么API方法以及我应该如何使用?

我找到了更优雅的解决方案,只使用headers_in获取REMOTE_ADDR:

use Apache2::Request;

my $r = shift;
my $req = Apache2::Request->new($r);
my $remip = $req->headers_in->{REMOTE_ADDR};

print $remip;

1 个答案:

答案 0 :(得分:2)

只需使用%ENV

即可
print $ENV{REMOTE_ADDR};

另外还有Apache2::Connection->remote_addr()

use Apache2::Connection ();
use Apache2::RequestRec ();

my $c = $r->connection;

# this connection's local and remote socket addresses
my $local_sa  = $c->local_addr();
my $remote_sa = $c->remote_addr();