Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Time Added In File

Posted on 2002-06-01
5
Medium Priority
?
213 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 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

688 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