Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

question 1

Status
Not open for further replies.

Cloud1

Programmer
Jun 23, 2001
6
CA
I have questions:

1. How do I print current time? Like the following format: 12:09:45

2. How do I get input from a form as well as writing to a file? (<>> won't work)

Thank you. :)
 
First question:
to print the current date and time (from the server) you can use the built in function localtime:

$mydate = localtime(time);

this will produce a string in the format:

Sun Jun 24 08:03:23 2001

and you can hack it around as you see fit.

second question:

data from a form comes in two flavours:
GET and POST the way i found is better, is to deal with the both in every script, to cover yourself.
This way i can cut and paste the following code what ever the script i'm writing is going to deal with:

my $in;
if ($ENV{'REQUEST_METHOD'} eq &quot;GET&quot;) {
$in = $ENV{'QUERY_STRING'};
} else {
$in = <STDIN>;
}

$in =~ s/\+/ /gi;
$in =~ s/%(..)/pack(&quot;c&quot;,hex($1))/ge;

now we have the data from the form that called the script in the varbile $in, now for the clever stuff

@data_in = split (/&/, $in);

for($i = 0; $i <= $#data_in; $i++)
{
@pairs = split (/=/, $data_in[$i]);
$data{$pairs[0]} = $pairs[1];
}

this lovey for loop makes life that much easier, it takes each name=value pair (and form data comes in this format) and makes it $data{name} = value;

now yu have all your data in the aso array %data and labeled by its form name. Handy no?

writing to a file? this is fun :p again we have two flavours, deleting any current file content, and writing a fresh file, or appenending an exisiting one (adding ot the end of it)

open(MEFILE, &quot;>myfile.dat&quot;);

print MEFILE &quot;$what_I_want_to_write&quot;;

close(MEFILE);

that opens a file, deletes whatever it holds, and writes to it,

open(MEFILE, &quot;>>myfile.dat&quot;);

print MEFILE &quot;$what_I_want_to_write&quot;;

close(MEFILE);

that opens a file, and adds to the end of it, note the extra >

its a good idea to use a || die &quot;Didn't open $!&quot;; on the end when you open a file, so you can easily debug if it doesn't work :)

Good luck, hope this helps.

Sib

Siberdude
siberdude@settlers.co.uk
 
Thanks! I'll try the form method later.

I tried the localtime thing before, but I don't really like it because it doesn't show it in the 12hr format. Can you tell me how to do that? Thanks again. :-D
 
UGH... I can't get the form parts to work. :-(

I'm making an admin logging part on my script and it looks like:

open (FILE, &quot;>>/home/puremadnezz/cgi-bin/test/variables/vars_log.cgi&quot;) or die(&quot;Unable to open vars_log.cgi file for writing.&quot;);
flock(FILE,LOCK_EX);
print FILE qq!$Gen|username|password|$Date|$IP|$Host\n!;
flock(FILE,LOCK_UN);
close (FILE);
chmod(0777, &quot;/home/puremadnezz/cgi-bin/test/variables/vars_log.cgi&quot;);

The login part of my script looks like:

<TABLE BORDER=&quot;1&quot;>
<FORM METHOD=&quot;POST&quot; ACTION=&quot;admin.cgi&quot;>
<INPUT TYPE=&quot;HIDDEN&quot; NAME=&quot;action&quot; VALUE=&quot;login&quot;>
<tr bgcolor=&quot;#FFFFFF&quot;>
<td align=&quot;center&quot;><FONT SIZE=&quot;2&quot; FACE=&quot;Verdana, Arial&quot;><B>Username</B></FONT></td>
<td align=&quot;center&quot;><INPUT TYPE=&quot;TEXT&quot; NAME=&quot;username&quot; SIZE=25 MAXLENGTH=40></td>
</tr>

<tr bgcolor=&quot;#FFFFFF&quot;>
<td align=&quot;center&quot;><FONT SIZE=&quot;2&quot; FACE=&quot;Verdana, Arial&quot;><B>Password</B></FONT></td>
<td align=&quot;center&quot;><INPUT TYPE=&quot;PASSWORD&quot; NAME=&quot;password&quot; SIZE=25 MAXLENGTH=40></td>
</tr>
</table>

What am I doing wrong...? :-(
 
As far as i can see there, the only problem is your appending the file, not writing to it, so its adding to what was already in the file.
Having worked on a IIS server for the most part of my time, i never used flock or chmod, why are you using them here?
you can write to your own dat files without all that messing around.

also i'd be tempted to put the vars you want to write, in speechmarks like so:

print FILE &quot;qq!$Gen|username|password|$Date|$IP|$Host\n!&quot;;

thats about all i can come up with. Check on the CGI board for a goood login script i wrote for someone else (under Help! CGI-BIN NEWBIE&quot; or something i think

hope this helps
Sib


Siberdude
siberdude@settlers.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top