Forum Moderators: coopster & phranque

Message Too Old, No Replies

determining remote user ip with perl

         

macdar

8:20 am on Aug 12, 2007 (gmt 0)

10+ Year Member



Hi,

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.

macdar

4:40 pm on Aug 12, 2007 (gmt 0)

10+ Year Member



ok, I'm getting closer now:
I enabled cgi and the module does seem to work fine..

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.

perl_diver

7:43 pm on Aug 12, 2007 (gmt 0)

10+ Year Member



it seems to me since you are calling a perl script from a local php script, the perl script will see the IP address of your server because the request comes from your server. Try runing a perl script directly from a url on your server and see what gets printed, example script:

#!/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

macdar

4:11 pm on Aug 13, 2007 (gmt 0)

10+ Year Member



perl diver,

you're abolutely right. I just fired up the script and it retrieves the right ip.

so, how should I pass on that perl script output (name of country) to my php code? (since the php's Perl class doesn't really do the job..)

perl_diver

5:10 pm on Aug 13, 2007 (gmt 0)

10+ Year Member



You could try capturing the ENV variable(s) you are interested in with your PHP script and send them to your perl script as a parameter or argument in the perl->require() function.

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

macdar

12:24 pm on Aug 16, 2007 (gmt 0)

10+ Year Member



perl diver,

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

perl_diver

7:01 pm on Aug 16, 2007 (gmt 0)

10+ Year Member



You should ask how to resolve that problem in the PHP forum, probably something simple, but I am not familiar with PHP.

macdar

12:59 pm on Aug 19, 2007 (gmt 0)

10+ Year Member



ok, perl_diver.

thanks for helping me on that issue.