View Full Version : Non-working CGI XML guestbook
Jonhoo
12-17-2005, 05:19 PM
Hi, I've just installed an Apache server and ActivePerl on my Windows XP computer. I've also written a guestbook in Perl/CGI, which I want to test. When I put it in my web directory and try to access it through my web browser, I get the following error:
syntax error at line 1, column 14, byte 14 at C:/usr/site/lib/XML/Parser.pm line 187
I have also tried executing a script which I know is working which also uses XML::Simple and XML::Parser, but then I get this error message:
no element found at line 1, column 0, byte 0 at C:/usr/site/lib/XML/Parser.pm line 187
You can find the script sourcecode at:
http://jon2.is-a-geek.com/gjestebok2.txt <-- This is my script
http://jon2.is-a-geek.com/guestbooksource.txt <-- This is the foreign script which I know is working
Can any of you spot the error?
I've also tried downloading the newest version of XML::Parser and XML::Simple from the CPAN website and copying them into the correct Perl folder, but no luck..
The cgi files are located:
http://jon2.is-a-geek.com/cgi-bin/gjestebok2 <-- This is my script
http://jon2.is-a-geek.com/cgi-bin/guestbook <-- This is the foreign script which I know is working
Help much appreciated
Jon
FishMonger
12-17-2005, 05:45 PM
I've also tried downloading the newest version of XML::Parser and XML::Simple from the CPAN website and copying them into the correct Perl folder, but no luck.I haven't looked at the scripts yet, but this may be part of the problem. due to dependencies, most modules need to be 'installed' not simply copied. My first recommendation would be to use PPM to install the modules, which also installs their supporting documentation.
FishMonger
12-17-2005, 06:42 PM
I see a possible problem with:chdir("cgi-bin");
my $error_data = $xml->XMLin("../gjestebok.xml");
Since you're not checking the status of the chdir, the $xml->XMLin call might have a problem in accessing the gjestebok.xml file.
It would help to troubleshoot if you use proper indentation. All of your if/else blocks are at the same indentation level.
Syntax issue:
Scalar value @fields[0] better writtes as $fields[0]
Scalar value @fields[1] better writtes as $fields[1]
Scalar value @fields[2] better writtes as $fields[2]
Jonhoo
12-17-2005, 07:04 PM
I´ve now fixed everything you mentioned, the files are still located in the same place. The comments have been translated as well. In the gjestebok2 file (jon2.is-a-geek.com/gjestebok2.txt)
The gustbook file (jon2.is-a-geek.com/guestbooksource.txt) has not been edited because I KNOW it´s supposed to work..
I don´t think it should have any problem accessing the .xml file, but how can I check?
When it comes to installing packages, the XMl packages originally comes with the Active Perl installation, but they didn´t work.. I can´t install it with ppm because then I get the error "XML::Parser is already installed".. If I try "upgrade XML::Parser" it just prints a newline and then I get the "ppm>" prompt again.
Using CPAN "force install XML::Parser" produces the error that it doesn´t find expat (Which I know is installed), and that it doesn´t find som .lib files (Which I can´t seem to find either).
Jon
FishMonger
12-18-2005, 07:34 PM
The gustbook file (jon2.is-a-geek.com/guestbooksource.txt) has not been edited because I KNOW itīs supposed to work.."supposed to work" is not the same as "it works".
the XMl packages originally comes with the Active Perl installation, but they didnīt work..Are you saying the the modules don't work? I have a hard time believing that's the case. It's actually either bugs in your script, invalid data (format) in the xml file, or a server misconfiguration.
You should step through the script by running it through the perl debugger. This will help show you what it's actually doing and where and when the error shows up. The error seems to indicate that the first line of the input xml data is invalid. Have you taken a close look at the xml file?
Jonhoo
12-18-2005, 08:09 PM
I KNOW the guestbook script (jon2.is-a-geek.com/guestbook.txt) is functional bacause the exact same code works on the server of a friend of mine.. It is however fully possible that the gjestebok2 script doesn't work.. I don't know.. This is why I tried to create a server, to check it out.. :)
The Perl Debugger? How?
I'll try to reinstall the whole Perl installation to make sure there are no errors on that part.. :)
Jon
BTW: How did you say I could check whether chdir works?
FishMonger
12-18-2005, 08:26 PM
BTW: How did you say I could check whether chdir works?There are several methods, but the most common would be to use die or your own custom error handling subroutine.
chdir("cgi-bin") || die "could not cd to cgi-bin $!";
Jonhoo
12-18-2005, 08:31 PM
Hehe, I just looked at my folder structure, and discovered that I don't need the whole
chdir
line at all :p
The code doesn't work anyways though.. Same error :(
FishMonger
12-18-2005, 09:09 PM
I thought that might be the case. I couldn't see why you set your structure to:
./cgi-bin/cgi-bin
To use the debugger, you'd execute the script from the command line.
perl -d gjestebok2.pl
I prefer to use an IDE for development, which includes the debugger, syntax highlighing/checking, command output, HTML output, and many other features.
http://activestate.com/Products/Komodo/?mp=1
Jonhoo
12-18-2005, 10:00 PM
I'll give Komodo a try as soon as I get the chance :)
I tried to run the debugger, but all I got was:
main::(gjestebok2:10): my $xml = new XML::Simple;
DB<1>
What to do now?
I appreciate all the help :)
Jon
FishMonger
12-18-2005, 10:25 PM
Try these 2 commands to get info on the use of the debugger.
C:\>perldoc perldebug
C:\>perldoc perldebtut
Jonhoo
12-18-2005, 10:51 PM
I got nothing out of the debugging... :rolleyes:
At least, nothing I understood, even after reading the tutorial..
Would you mind running through it?
Jon
FishMonger
12-18-2005, 11:03 PM
In order to test it, I'd need a copy of your gjestebok.xml file.
Jonhoo
12-18-2005, 11:05 PM
It is currently empty except for a test post which I have written from scratch..
You can find it at http://jon2.is-a-geek.com/gjestebok.xml
FishMonger
12-18-2005, 11:13 PM
Well, the first thing I see is you're missing
<?xml version="1.0"?>
That line isn't always required, but it is in most cases.
Jonhoo
12-18-2005, 11:18 PM
I just sort of made it work!!
Some small modifications here and there, and now it works in command line, but not on the web...
The half working source code is now updated
FishMonger
12-18-2005, 11:32 PM
I haven't run it through the debugger yet, but as a quick test I added a couple debugging statements. This is what I ran.#!/usr/bin/perl
#gjestebok
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use Socket;
use strict;
use XML::Simple;
use Data::Dumper;
my $xml = new XML::Simple;
my $cgi=new CGI();
my $error_data = $xml->XMLin("../gjestebok.xml");
print "\$error_data is a ", ref $error_data, " reference\n\n";
my $database_error_results;
my $error_post;
foreach $error_post (@{$error_data->{post}})
{
$database_error_results .= "Navn: $error_post->{name}<br>";
if ($ENV{"REMOTE_ADDR"} eq inet_ntoa(inet_aton("jon2.is-a-geek.com")))
{
$database_error_results .= "IP: $error_post->{IP}<br>";
}
$database_error_results .= "E-post: $error_post->{epost}<br><br>";
$database_error_results .= qq($error_post->{melding}<br><br>);
$database_error_results .= qq(<i>$error_post->{tid}</i><p></p>);
$database_error_results .= qq(<tr><td colspan="5"><hr size="1" noshading></td></tr>);
$database_error_results .= "<br>";
}
print Dumper $database_error_results and exit;
This is the output I got.
$error_data is a HASH reference
<h1>Software error:</h1>
<pre>Not an ARRAY reference at J:\temp\gjestebok2.pl line 20.
</pre>
<p>
For help, please send mail to this site's webmaster, giving this error message
and the time and date of the error.
</p>
[Sun Dec 18 14:26:57 2005] gjestebok2.pl: Not an ARRAY reference at J:\temp\gjestebok2.pl line 20.
FishMonger
12-18-2005, 11:40 PM
I probably should have dumped the hash as well.
$VAR1 = {
'post' => {
'Jon' => {
'IP' => '12.13.14.15',
'epost' => 'jon_bros@hotmail.com',
'message' => 'test',
'tid' => '01:23 den 4. i 5.'
}
}
};
Jonhoo
12-18-2005, 11:44 PM
I get the error:
Can't use string ("bruker") as a HASH ref while "strict refs" in use at C:/Documents and Settings/Jon.JONSPC.000/Mine dokumenter/Min hjemmeside/cgi-bin/gjestebok2 line 21.
when I try to run the script.. New sourcecode is located at http://jon2.is-a-geek.com/gjestebok2.txt
Jonhoo
12-18-2005, 11:52 PM
Now I made the whole script work locally on my computer, but I can't seem to make it work online.. :(
gjestebok2.txt is updated again
FishMonger
12-19-2005, 12:09 AM
As I see it, the problem is in how you're dereferencing the hash.
For testing purposes, lets drop the web output section and only work on the parsing of the xml file. I'm running the tests using Komodo and exiting just prior to printing the headers.
If you add in the Dumper print statements, you'll see that there isn't a hash key "bruker", at least not in the xml file that you posted. When I run the script and dump the vars, this is what I get.
print Dumper $error_data;
$VAR1 = {
'post' => [
{
'IP' => '12.13.14.15',
'name' => 'Jon',
'epost' => 'jon_bros@hotmail.com',
'tid' => '01:23 den 4. i 5.',
'message' => 'test'
},
{
'IP' => '12.13.14.15',
'name' => 'Jon',
'epost' => 'jon_bros@hotmail.com',
'tid' => '01:23 den 4. i 5.',
'message' => 'test'
}
]
};
print Dumper $database_error_results;
$VAR1 = 'bruker: <br>
E-post: jon_bros@hotmail.com<br><br>
test<br><br>
<i>01:23 den 4. i 5.</i><p></p>
<tr><td colspan="5"><hr size="1" noshading></td></tr>
<br>
bruker: <br>
E-post: jon_bros@hotmail.com<br><br>
test<br><br>
<i>01:23 den 4. i 5.</i><p></p>
<tr><td colspan="5"><hr size="1" noshading></td></tr>
<br>
';
Jonhoo
12-19-2005, 12:12 AM
Once again, I've updated all the files, and put them excactly where they where before:
XML file (http://jon2.is-a-geek.com/gjestebok.xml)
Script (http://jon2.is-a-geek.com/cgi-bin/gjestebok2)
Sourcecode (http://jon2.is-a-geek.com/gjestebok2.txt)
My only problem now is that nothing shows if I try to access the script online... If I run it through Komodo or perl command line, I get the correct output.. :)
Jon
FishMonger
12-19-2005, 12:24 AM
The link to your script doesn't have a .pl or .cgi extention. Do you have your web server configured to execute the scripts without the extension? Try adding some addtional debugging print statements.
Jonhoo
12-19-2005, 12:25 AM
Yeah, it's supposed to execute everything in the cgi-bin folder... I've tried adding extensions, but with no luck.. :(
FishMonger
12-19-2005, 12:30 AM
What happens when you add the .txt extension? Does it display the source code.
Jonhoo
12-19-2005, 12:31 AM
Nope.. Just the same happens as with any other extension..
FishMonger
12-19-2005, 12:41 AM
Your script for the random quotes is working, so there must be something unusual going on with this script, but I don't see anything too odd.
Try:
print $cgi->header, $cgi->start_html();
print $cgi->h1("Error Results");
and at the end:
print $cgi->end-html();
Jonhoo
12-19-2005, 12:45 AM
A page which says:
Error Results
Jonhoo
12-19-2005, 12:56 AM
I fixed it! :D
Sourcecode is released...
I simply had to make my script print a special page if no cgi input parameters were passed :D
Thanks :)
Jon
FishMonger
12-19-2005, 01:00 AM
Good, we've verified that the script is executing. Now you need to look at the logic that you're using in the if/else blocks and make sure that you have some sort of print statements in each block and a closing print statement at the end.
Jonhoo
12-19-2005, 01:20 AM
The script now works completely :D
Check it out at http://jon2.is-a-geek.com/cgi-bin/gjestebok2
Thanks :)
Jon
FishMonger
12-19-2005, 02:07 AM
Good work. Now that it's working, you may want to make it better. Your method of verifing the email format could use some work. That regex will allow lots of invalid addresses and disallow some valid ones.
http://search.cpan.org/~maurice/Email-Valid-0.15/Valid.pm
http://search.cpan.org/~sonnen/Data-Validate-Email-0.03/Email.pm
http://search.cpan.org/~jsiracusa/Rose-HTML-Objects-0.30/lib/Rose/HTML/Form/Field/Email.pm
Jonhoo
12-19-2005, 07:22 AM
Done :)
Works great :)
Any further suggestions?
Should I maybe use HTML::Template? And if so, how?
Jon
FishMonger
12-19-2005, 07:28 AM
I'd first look at using an SQL database instead if the xml file, then maybe move to HTML::Template or other templating system.
http://search.cpan.org/~timb/DBI-1.50/DBI.pm
Jonhoo
12-19-2005, 07:30 AM
I tried looking at that, but it became to complicated without a proper guide... Simply reading the syntax just got confusing... :)
Do you know any good HTML::Template and SQL tutorials?
BTW: Inputed text is now preserved even though an error occurs..
Jon
FishMonger
12-19-2005, 07:45 AM
Here are some of the books that I'd recommend. After I get some sleep, I'll work up some online sources.
http://www.oreilly.com/catalog/perltt/
http://www.oreilly.com/catalog/perlhtmlmason/
http://www.oreilly.com/catalog/perlxml/
http://www.oreilly.com/catalog/perldbi/
http://www.oreilly.com/catalog/perlckbk2/
http://www.oreilly.com/catalog/pperl3/
Jonhoo
12-19-2005, 08:29 AM
Thanks :)
I really appreciate your help :D
Jon
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.