Solved

Time Added In File

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

756 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