Solved

Time Added In File

Posted on 2002-06-01
5
191 Views
Last Modified: 2010-03-05
A Followup to this:

http://experts-exchange.com/jsp/qManageQuestion.jsp?ta=perl&qid=20306786

The answer was:

#!/usr/bin/perl
use CGI;
use strict;

my $datafile = 'text.txt';
my $q = new CGI;
my $data = $ENV{'QUERY_STRING'};

open (FILE, ">>$datafile");
print FILE "$data\n";
close(FILE);

print <<END;
<HTML><title>one second</title></head><META HTTP-EQUIV="Refresh" Content=0;URL="http://www.mydomain.com"></HEAD><BODY></BODY></HTML>
END
;



Can I have the script add the date and time of the HIT right after the QUERY_STRING ? Thanks!
0
Comment
Question by:phita
  • 3
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
yoren earned 500 total points
ID: 7049053
Sure. The key perl function is "localtime(time)". You can also use POSIX and strftime. Here are two examples.

Example 1 using localtime(time):

#!/usr/bin/perl
use CGI;
use strict;

my $datafile = 'text.txt';
my $q = new CGI;
my $data = $ENV{'QUERY_STRING'};

my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
    = localtime(time);

open (FILE, ">>$datafile");

printf FILE "%s\t%02d/%02d/%04d %02d:%02d:%02d\n",
       $data,$mon+1,$mday,$year+1900,$hour,$min,$sec;

close(FILE);

print <<END;
<HTML><title>one second</title></head><META HTTP-EQUIV="Refresh" Content=0;URL="http://www.mydomain.com"></HEAD><BODY></BODY></HTML>
END
    ;


Here's another example using POSIX to construct a time stamp similar to the one used in Apache logs:

#!/usr/bin/perl
use CGI;
use POSIX qw(strftime);
use strict;

my $datafile = 'text.txt';
my $q = new CGI;
my $data = $ENV{'QUERY_STRING'};
my $timestamp = strftime("[%e/%b/%Y:%T %z]", localtime);

open (FILE, ">>$datafile");
print FILE "$data\t$timestamp\n";
close(FILE);

print <<END;
<HTML><title>one second</title></head><META HTTP-EQUIV="Refresh" Content=0;URL="http://www.mydomain.com"></HEAD><BODY></BODY></HTML>
END
    ;

0
 

Author Comment

by:phita
ID: 7049148
ERROR for first one:
Can't find string terminator "END" anywhere before EOF at d:\html\users\rintoncom\html\xiao\test.pl line 19.


ERROR for second one:
Can't find string terminator "END" anywhere before EOF at d:\html\users\rintoncom\html\xiao\test.pl line 15.
0
 

Author Comment

by:phita
ID: 7049152
ERROR for first one:
Can't find string terminator "END" anywhere before EOF at d:\html\users\rintoncom\html\xiao\test.pl line 19.


ERROR for second one:
Can't find string terminator "END" anywhere before EOF at d:\html\users\rintoncom\html\xiao\test.pl line 15.
0
 
LVL 7

Expert Comment

by:yoren
ID: 7049167
Sounds like a copy/paste problem. Make sure there are no spaces before the "END" line, so that END is the first thing on that line.
0
 

Author Comment

by:phita
ID: 7049221
Great, thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question