Reputation: 465
I am trying to get visitors' ip address logged. I can do it fine with PHP, but when I try this Perl code (novice in Perl), when a visitor goes to my site, it logs only my website's host ip address (in this case tripod) as such: 19Apr11 20:25:35 10.126.24.9. But that's not what I want; I want the visitor's ip address? what can I be doing wrong here? Thanks
#!/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
Upvotes: 1
Views: 8836
Reputation: 2185
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
Upvotes: 5