Solved

Time Added In File

Posted on 2002-06-01
5
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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…

738 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