Solved

Time Added In File

Posted on 2002-06-01
5
171 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

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 …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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 video discusses moving either the default database or any database to a new volume.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now