Logs visitors to web site : Page « CGI « Perl






Logs visitors to web site

    

use strict;
use warnings;
use CGI qw( :standard );
use Fcntl qw( :flock );

my @vars = qw( REMOTE_ADDR REMOTE_PORT REQUEST_URI QUERY_STRING );
my @stuff = @ENV{ @vars };
my $info = join( " | ", @stuff );

open( FILE, "+>>log.txt" ) or die( "Could not open log.txt: $!" );
flock( FILE, LOCK_EX ) or die( "Could not get exclusive lock: $!" );
print( FILE "$info\n\n" );
flock( FILE, LOCK_UN ) or die( "Could not unlock file: $!" );

close( FILE );

if ( $stuff[3] ne "" ) {
   print( header( -Refresh=> '5; URL=http://www.java2s.com' ) );
   print( start_html( "log.txt" ) );
   print( p( i( "You will now be redirected to our home page." ) ) );
}
else {
   print( header() );
   print( start_html( "log.txt" ) );
   print( h1( "Please add a \"?\" and your name to the URL.\n" ) );
}

print( end_html() ); 

   
    
    
    
  








Related examples in the same category

1.Track the number of times a web page has been accessed
2.Redirect a web page
3.Redirect page
4.Values Carried Between Pages
5.Page counter
6.Server push