I need to check users' ip addresses using perl.
I googled for solutions to accomplish that, and the only one I have found uses environmental variable:
$ENV{'remote_addr'}
it does not work for me. When I dump that hash, I got the following:
$VAR1 = { 'SHLVL' => '1', 'PWD' => '/usr/lib/php5/20051025', '_' => '/usr/sbin/apache2', 'LANG' => 'C', 'PATH' => '/usr/local/bin:/usr/bin:/bin' };
BTW, I'm new to perl, and trying to use a script along with my php code. I have recompiled my php so it can execute perl code(pecl's perl extension). I could easily retrieve an ip with php, however I'm not sure if I can pass it to the external perl script. that's what I do:
$perl = new Perl();
(I could assign a perl var here, but it's not being passed to an external file, so I need to get the ip in my perl script)
$perl->require("get_country.pl");
$out = ob_get_contents();
ob_end_clean();
print $out;
that's the tutorial that shows the basics:
[devzone.zend.com...]
anyone has ever done anything similar?
thanks.
but the value of $ENV{'REMOTE_ADDR'} always returns my domain's ip, instead of user's ip. it looks like something somewhere is misconfigured (is it an apache thing?), but not sure where to look for solutions.
BTW, php's $_SERVER['REMOTE_ADDR'] works fine.
also, my resolv.conf file includes:
search localhost
nameserver 10.0.80.11
nameserver 10.0.80.12
thanks.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
foreach my $keys (sort keys %ENV) {
print "$keys = $ENV{$keys}<br/>\n";
}
save in plain text as env.pl, upload into your cgi script enabled folder in ascii (text) mode, chmod to 755 if necessary, and run it from a browser using the url:
http://www.example.com/cgi-bin/env.pl
With perl you use the @ARGV array to get the arguments passed to it.
perl->require("script.pl $var1 $var2 $var3");
script.pl:
my $ip = $ARGV[0];
my $foo = $ARGV[1];
etc
etc
etc
thanks for the suggestion. I did as you said:
<?php
ob_start();
$perl = new Perl();
$ip = "72.14.207.99";
$perl->require("cgi-bin/test.pl {$ip}");
$out = ob_get_contents();
ob_end_clean();
print "Perl: $out";
?>
but an exception has been caught. it looks like you cannot pass on params with that function:
Fatal error: Uncaught exception 'PerlException' with message '[perl] require error: Can't locate cgi-bin/test.pl 72.14.207.99 in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.8.7 /usr/local/share/perl/5.8.7 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl .) at (eval 1) line 1. ' in /var/www/toplist/php_perl.php:6 Stack trace: #0 /var/www/toplist/php_perl.php(6): Perl->require('cgi-bin/test.pl...') #1 {main} thrown in