2011-04-19 6 views
1

나는 방문자의 IP 주소를 기록하려고한다. 나는 PHP로 잘 할 수 있지만 Perl 코드 (초보자 인 Perl)를 사용하면 방문자가 내 사이트로 이동하면 내 웹 사이트의 호스트 IP 주소 (이 경우에는 삼각대) 만 다음과 같이 기록됩니다. 19Apr11 20:25 : 35 10.126.24.9. 그러나 그것은 내가 원하는 것이 아닙니다. 방문자의 IP 주소를 원해? 내가 여기서 뭘 잘못 할 수 있니? 감사합니다IP 주소를 얻는 펄 코드

#!/usr/bin/perl 
# For logging IP address of visitors. 
# This is stored into the cgi-bin folder and called from the index.htm file 
# using <img src="cgi-bin/script.pl"> 
# Specify location and name of log file to be maintained. 
my $LogFileLocation = "iplog.txt"; 
# Directory must exist and have correct permissions. 
use strict; 
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; 
my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); 
$year += 1900; 
$year = substr($year,-2); 
$mday = $mday < 10 ? "0$mday" : $mday; 
$hour = $hour < 10 ? "0$hour" : $hour; 
$min = $min < 10 ? "0$min" : $min; 
$sec = $sec < 10 ? "0$sec" : $sec; 
my $address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} ||""; 
open W,">>$LogFileLocation"; 
print W "$mday$mon[$mon]$year $hour:$min:$sec\t$address\n"; 
close W; 
# end of script 
+5

모든 날짜 코드를 단순화 할 수있다 :'POSIX QW (의 strftime)를 사용; – toolic

답변

5
use CGI; ## load the cgi module 

print "Content-type: text/plain; charset=iso-8859-1\n\n"; 
my $q = new CGI; ## create a CGI object 
print $q->remote_host(); ## print the user ip address 
+0

여전히 좋지 않음 : 1) 위와 동일하게 방문자의 주소가 아니라 호스트의 주소를 출력합니다. 2) 그렇지 않습니다. 로그 파일로 인쇄하십시오. – francogrex

+0

@francogrex : 다시 시도하십시오! remote_host() 또는 $ ENV {REMOTE_ADDR}이 (가) 지금은 약 13 년 동안 나에게 도움이됩니다. –

+0

이상한! 나를 위해서가 아니야. 나는 여전히 로컬 호스트 ip (10.126.24.9)를 얻는다. 어쩌면 뭔가를 차단하고있는 호스트 일 수도 있습니다. – francogrex

관련 문제