[wplug] Perl script - need help

Brian Medley pub-lists-wplug at bmedley.org
Wed Jan 23 15:21:18 EST 2008


On Wed, Jan 23, 2008 at 02:36:54PM -0500, Zach wrote:

In addition to what others have said you might try using CGI.pm
or others for the headers and whatnot.  Also, warning and strict
checks are helpful (you'll have to pre-declare variables this way
with "my").

#!/usr/bin/perl

use strict;
use warnings;

use CGI;

...

my ($ip, $agent);

print(&CGI::header());
print("Your Ip was logged.....$ip<br><br>\n");
print("Your Agent is..........$agent<br><br>\n");

To help out with some of the open() modes you can utilize:

    $ perldoc -f open

> Writing my first CGI script (also my first Perl script!) and I'm
> getting some unexpected results.
> 
> First probem:
> 
> The ip and browser type etc are printed out in the browser on one line
> with no newline yet I included "\n\n"
> 
> Second problem:
> 
> In my log file the ip is printed on first line, then newline, and on
> 3rd line is the browser type etc.  This is all good, except
> each time the script is run it over-writes the previous information.
> So instead of making lines 4,5,6 it just over-writes lines 1,2,3
> 
> Any help in debugging this would be appreciated.
> 
> Here is my script:
> 
> #!/usr/bin/perl
> # script to log IP and other interesting visitor info
> 
> $log_file = "/afs/club.cc.cmu.edu/usr/chaos/www/test/ip.foo8.txt";
> $ip = $ENV{'REMOTE_ADDR'};
> $agent = $ENV{'HTTP_USER_AGENT'};
> $fp = open (LOG, ">$log_file"); # opens the log file for writing
> flock(LOG, 2); # file lock set
> print LOG "$ENV{'REMOTE_ADDR'} \n\n";
> print LOG "$ENV{'HTTP_USER_AGENT'} \n\n";
> flock(LOG, 8); # file lock unset
> close(LOG);
> print "Content-type: text/html\n\n";
> print "Your Ip was logged.....$ip\n\n";
> print "Your Agent is..........$agent\n\n";
> 
> Zach
> _______________________________________________
> wplug mailing list
> wplug at wplug.org
> http://www.wplug.org/mailman/listinfo/wplug

-- 
Brian Medley


More information about the wplug mailing list