Perl服务端通过CGI获取客户端ip地址

阅读更多

use CGI;

 

my $remoteip = $ENV{"REMOTE_ADDR"};   --> 客户端IP地址

my $remoteport = $ENV{"REMOTE_PORT"};  --> 端口

my $http_via = $ENV{"HTTP_VIA"};  --> VIA头部

 

可以根据下面方式,查看整个http头部内容:

my $ua = LWP::UserAgent->new();

$ua->timeout(30); 

#$ua->proxy(['http'], "http://$proxyAddr");

 my $req = HTTP::Request->new( GET => $url);

$req->header(@header);

#print(Dumper($req)."\n");

my $response = $ua->request($req);

print($response->decoded_content());  #将打印下面的内容

 

 

       

   

     

        ????

       

     

 

      1

HTTP_VIA===>1.1 RD2:3128 (squid/2.7.STABLE7)

HTTP_ACCEPT_CHARSET===>utf-8, iso-8859-1, utf-16, *;q=0.7

PATH===>/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

CONTEXT_DOCUMENT_ROOT===>/usr/lib/cgi-bin/

SERVER_SOFTWARE===>Apache/2.4.7 (Ubuntu)

REMOTE_PORT===>40843

MOD_PERL_API_VERSION===>2

SERVER_ADMIN===>webmaster@localhost

REQUEST_URI===>/cgi-bin/check.pl?uid=1

HTTP_X_REQUESTED_WITH===>com.android.browser

SERVER_SIGNATURE===>

Apache/2.4.7 (Ubuntu) Server at 104.5.10.13 Port 80

HTTP_ACCEPT===>text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

SCRIPT_NAME===>/cgi-bin/check.pl

SERVER_NAME===>104.5.10.13

GATEWAY_INTERFACE===>CGI/1.1

HTTP_CONNECTION===>keep-alive

SERVER_ADDR===>104.5.10.13

REQUEST_SCHEME===>http

QUERY_STRING===>uid=1

SERVER_PORT===>80

HTTP_HOST===>104.5.10.13

REQUEST_METHOD===>GET

SCRIPT_FILENAME===>/usr/lib/cgi-bin/check.pl

HTTP_CACHE_CONTROL===>max-age=0

CONTEXT_PREFIX===>/cgi-bin/

DOCUMENT_ROOT===>/var/www/html

HTTP_X_FORWARDED_FOR===>106.6.6.104

HTTP_USER_AGENT===>Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; HM NOTE 1LTE Build/KTU84P)

REMOTE_ADDR===>58.22.61.211

SERVER_PROTOCOL===>HTTP/1.0

MOD_PERL===>mod_perl/2.0.8

IP===>58.22.61.211

Type===>PUBLIC

IS_PROXY===>TRUE1.1 RD2:3128 (squid/2.7.STABLE7)

   

 

可以通过HTTP_VIA(是否存在)或者IS_PROXY(是否包含TRUE)来判断客户端是否使用了代理来。

你可能感兴趣的:(perl,cgi,linux)