Web Hosting Talk







View Full Version : cgi.pm guestbook help


acidhoss
12-03-2002, 09:50 PM
Allo again
I had to reboot my comp a little while ago, so I wrote this new guestbook in perl using a tutorial from about.com using cgi.pm. I'm not really familiar with cgi.pm, or any other perl modules for that matter, so i was wondering if someone could help me with this problem i am having:
when i run it, it seems to work fine, it prints out the form and everything, but when i submit it, nothing happens. So, i checked my apache error log file, and it said this:
"[Tue Dec 03 15:12:18 2002] [error] [client 127.0.0.1] Can't open GUESTBOOK:C:/perl/htdocs/guestbook.html: No such file or directory"
I restarted apache, tried it again, and then it said this:
"[Tue Dec 03 15:20:30 2002] [error] [client 127.0.0.1] File does not exist: c:/perl/htdocs/guestbook.htm"

weird, huh?
Here is the script, if some one could help me by pm me or just responding to this, that would be great.
Btw, i am running indigo perl mod_perl ver. 1.25 on windows me.
THE SCRIPT:

#!perl
##################################################
#Guestbook by dhoss of twisted hamster designs
#written with cgi.pm
#################################################


use strict;
use CGI;

#FIle path names


#relative (absolute, actually) path to guestbook file
my $guestbook = "C:/perl/htdocs/guestbook.html";

#guestbook url
my $guestbookurl = "http://oemcomputer/guestbook.html";

#initialize months for later use in date
my @month = ("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December");


#Start to use CGI.pm

my $query = new CGI();

print $query->header;
print $query->start_html('Add your name!');

#Create the user input form
print $query->start_form,
"Your name:", $query->textfield('name'),
$query->p,
"City:", $query->textfield('city'),
$query->p,
"Comments",$query->br,
$query->textarea(-name,=>'comments', -rows=>10,-columns=>50),
$query->p,
$query->submit('Add me!'),
$query->p,
$query->end_form,
$query->hr,
$query->p,


#ok, now get the date and time infor
my ($Seconds, $Minutes, $Hours, $DayInMonth, $Month, $ShortYear, $DayOfWeek, $DayOfYear, $IsDST);

($Seconds, $Minutes, $Hours, $DayInMonth, $Month, $ShortYear, $DayOfWeek, $DayOfYear, $IsDST) = localtime(time);

#Fix it to keep it Y2K compliant
my $Year = $ShortYear + 1900;
my $EntryDate = "$month[$Month]$DayInMonth,$Year";

#Open the guestbook file and read it into an array
open(GUEST,"$guestbook") || die "Can't open GUESTBOOK:$guestbook: $!\n";
my @GUEST = <GUESTBOOK>;
close(GUEST);

#Open it once more, this time to write to it
open(GUEST,">$guestbook") || die "Can't open GUESTBOOK:$guestbook: $!\n";



my $line;

foreach $line(@GUEST){
#Check and see if we are at the beginning of the guestbook file
if($line=~/<!--START:-->/i){
#We are, so start adding the entry
#Start with adding the header <!--START:--> so we can find where we are next time the guest
#book is signed
print GUEST "<!--START:-->\n";

#Make sure the user entered data to be written to the guestbook file
if($query->param('name')){
{

print GUEST "<DL>\n";

if($query->param('email')){
#if they gave and email address, add it as a link
print GUEST $query->em($query->b($query->a
({href=>"mailto:".$query->param('email')},
$query->param('name')) ) );
print GUEST $query->br;
}
else{
#otherwise, just add their name
print GUEST $query->em($query->b($query->param('name')));
print GUEST $query->br;
}
#add the city
if($query->param('city')){
print GUEST $query->em($query->param('city'));
print GUEST $query->br;}

#add the comments
if($query->param('comments')){
print GUEST $query->em($query->param('comments'));
print GUEST $query->br;}

#add the date
print GUEST $query->em("Signed on $EntryDate");

#finish it all up
print GUESTBOOK "</DL>\n";
print GUEST $query->hr;
}
}
else {
#just put the existing line in the file
print GUEST "$line";
}
}
close(GUEST);

print $query->h1('Thanks!');
print "Your entry has been added to the ", $query->a( {-href=>$guestbookurl}, 'guestbook'), ". You may need to click reload to view your guestbook entry.";
}



Help me with this noodle scratcher if ya can.
Gracias.

chrisb
12-04-2002, 12:31 AM
Originally posted by acidhoss
Allo again
I had to reboot my comp a little while ago, so I wrote this new guestbook in perl using a tutorial from about.com using cgi.pm. I'm not really familiar with cgi.pm, or any other perl modules for that matter, so i was wondering if someone could help me with this problem i am having:
when i run it, it seems to work fine, it prints out the form and everything, but when i submit it, nothing happens. So, i checked my apache error log file, and it said this:
"[Tue Dec 03 15:12:18 2002] [error] [client 127.0.0.1] Can't open GUESTBOOK:C:/perl/htdocs/guestbook.html: No such file or directory"
I restarted apache, tried it again, and then it said this:
"[Tue Dec 03 15:20:30 2002] [error] [client 127.0.0.1] File does not exist: c:/perl/htdocs/guestbook.htm"

weird, huh?
Here is the script, if some one could help me by pm me or just responding to this, that would be great.
Btw, i am running indigo perl mod_perl ver. 1.25 on windows me.


Try this...

1. ADD a -w to your perl path in order to see better error messages
#!perl -w

2. Use backslashes for windows directories, such as
my $guestbook = "C:\perl\htdocs\guestbook.html";

3. Use binmode for filehandles on a windows machine, such as:
binmode $guestbook;

4. Use the correct ACL for files and directories. Check your Access Control Lists. They should be set to Read, Write and Execute for Everyone, I believe (though I'm not real familiar with Windows ACLs).

I don't have time to troubleshoot the script completely, but it looks like you are trying to write to a closed filehandle. Also, shouldn't <GUESTBOOK> be <GUEST> ?

acidhoss
12-10-2002, 07:12 PM
WEll, thanks, i changed <GUESTBOOK> to <GUEST>, put the -w at the beginning, but now im not getting any errors, but the whole guestbook file is being erased. I even went as far as to ake the orignal script from the tutorial and test it, and it did the same thing. Any ideas??? I'll look into the windows ACL list, but i dunno, maybe cgi.pm is just being screwy.

Chas
12-11-2002, 10:29 AM
#Open it once more, this time to write to it
open(GUEST,">$guestbook") || die "Can't open GUESTBOOK:$guestbook: $!\n";


You are overwriting your file right here. Try appending to the file:


#Open it once more, this time to write to it
open(GUEST, '>>' . $guestbook) or die "Can't open GUESTBOOK:$guestbook: $!\n";


Regards,
Charlie

acidhoss
12-12-2002, 07:08 PM
That's what i thought, I will try it and see what happens
Thanks so much
hoss

acidhoss
12-14-2002, 01:51 AM
well,
it is writing to the file now, and doing everything right as far as i can tell, except it is writing to a more than once, if you know what i mean. For example, when i enter one entry, it will write it to the file 2 or 3 times. Any ideas?
Here's the new code:
#!perl -w
##################################################
#Guestbook by dhoss of twisted hamster designs
#written with cgi.pm
#################################################
#Hi! THis is my first guestbook with cgi.pm
#Please feel free to use this script, read the read me file
# and by all means, send any questions you have to
#puckchewer@cs.com
#have fun!!!
#################################################


use strict;
use CGI;

#FIle path names


#relative (absolute, actually) path to guestbook file
my $guestbook = "C:/perl/htdocs/guestbook/guestbook.shtml";
#my $guestbookurl = "http://oemcomputer/guestbook/guestbook.shtml";

#my $CSS = "<LINK rel='Stylesheet' type='text/css' href='http://oemcomputer/guestbook/css/guest.css'>";
#uncomment to use IP logging
#my $Number = "ENV{'REMOTE_HOST'}";
#Haven't tested to see if this works, so I just left it out
#IP log file
#my $IPLog = "C:/perl/htdocs/guestbook/logs/IP.dat";

#guestbook url
my $guestbookurl = "http://oemcomputer/guestbook/guestbook.shtml";

#initialize months for later use in date
my @month = ("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December");


#Start to use CGI.pm

my $query = new CGI();

print $query->header;

print $query->start_html('Add your name!');
#print $CSS;
#Create the user input form
print $query->start_form,
"Your name: ", $query->textfield('name'),
$query->p,
"E-mail address: ", $query->textfield('email'),
$query->p,
"City: ", $query->textfield('city'),
$query->p,
"Comments: ", $query->br,
$query->textarea(-name=>'comments', -rows=>10, -columns=>50),
$query->p,
$query->submit(),
$query->p,
$query->end_form,
$query->hr,
$query->p;


#ok, now get the date and time infor
my ($Seconds, $Minutes, $Hours, $DayInMonth, $Month, $ShortYear, $DayOfWeek, $DayOfYear, $IsDST);

($Seconds, $Minutes, $Hours, $DayInMonth, $Month, $ShortYear, $DayOfWeek, $DayOfYear, $IsDST) = localtime(time);

#Fix it to keep it Y2K compliant
my $Year = $ShortYear + 1900;
my $EntryDate = "$month[$Month]$DayInMonth,$Year";

#Open the guestbook file and read it into an array
open(GUEST,"$guestbook") || die "Can't open GUESTBOOK:$guestbook: $!\n";
my @GUEST = <GUEST>;
close(GUEST);

#Open it once more, this time to write to it
open(GUEST,">>$guestbook") || die "Can't open GUESTBOOK:$guestbook: $!\n";



my $line;

foreach $line(@GUEST){
#Check and see if we are at the beginning of the guestbook file
if($line=~/<!--START:-->/i){
#We are, so start adding the entry
#Start with adding the header <!--START:--> so we can find where we are next time the guest
#book is signed
print GUEST "<!--START:-->\n";

#Make sure the user entered data to be written to the guestbook file
if($query->param('name')){
{

print GUEST "<DL>\n";

if($query->param('email')){
#if they gave and email address, add it as a link
print GUEST $query->em($query->b($query->a
({href=>"mailto:".$query->param('email')},
$query->param('name')) ) );
print GUEST $query->br;
}
else{
#otherwise, just add their name
print GUEST $query->em($query->b($query->param('name')));
print GUEST $query->br;
}
#add the city
if($query->param('city')){
print GUEST $query->em($query->param('city'));
print GUEST $query->br;}

#add the comments
if($query->param('comments')){
print GUEST $query->em($query->param('comments'));
print GUEST $query->br;}

#add the date
print GUEST $query->em("Signed on $EntryDate");

#finish it all up
print GUEST "</DL>\n";
print GUEST $query->hr;
}
}
else {
#just put the existing line in the file
print GUEST "$line";
}
}


}
close(GUEST);
#print "Location: $guestbook\n\n";
print $query->h1('Thanks!');
print "Your entry has been added to the ", $query->a( {-href=>$guestbookurl}, 'guestbook'), ". You may need to click reload to view your guestbook entry.";