Click to See Complete Forum and Search --> : Need help installing cgi poll
penguine
02-20-2005, 04:23 PM
Okay, I've been working on installing the cgi poll that Joe did a tutorial on. I copied the script just as he had it. I only changed the perl path (which I made sure was correct), the third answer (only the names), and the result image locations. I made sure to put things in the right place. The images in a file outside my cgi-bin and the .cgi and .dat files inside. I made sure to chmod the .cgi and .dat files properly. And yet when I call up the page, I get a 404 when I call up the poll.cgi page. How do I fix this?
Nedals
02-21-2005, 12:56 PM
404 is 'page not found'
Probably your path to the script is wrong
"/cgi-bin/poll.cgi";
The first '/' starts the path at the webroot.
or
"http://www.domain.com/cgi-bin/poll.cgi";
for the full path.
penguine
02-21-2005, 01:10 PM
I did the full path. And I know it's correct. But the cgi-bin is acting like the page isn't there.
Nedals
02-21-2005, 01:37 PM
>> But the cgi-bin is acting like the page isn't there.
Then it isn't. :confused:
What is the directory structure on your server?
-domain(maybe)
-cgi-bin (here maybe)
-htdocs
-index.html
-images
-cgi-bin (or here maybe)
-poll.cgi (chmod 755)
-data.dat
How are you calling the script?
<a href="http://www.domain.com/cgi-bin/poll.cgi">Poll</a>
penguine
02-21-2005, 01:58 PM
Yes, it is.
Here's the directory path as seen in the file manager.
http://mindsjourney.net/temp/screensht.jpg
Here's my form post code.
<FORM METHOD="POST" ACTION="http://mindsjourney.net/cgi-bin/poll/poll.cgi">
And.. if you even try plugging the address of the poll.cgi into the address bar directly.. it will give the 404 page. (Warning.. page will literally scream at you.)
http://mindsjourney.net/cgi-bin/poll/poll.cgi
Nedals
02-21-2005, 04:04 PM
I'm not sure how you have your server set up to return that whoops.html page on a 404, 'not found' page. Maybe its returning that page on ANY error including a 500: (script) error. Have you any other cgi scripts that work?
I notice that if I enter 'http://mindsjourney.net/cgi-bin/' it returns that whoops.html page. What I would normally expect in a 403: 'not-authorized' page.
penguine
02-21-2005, 06:15 PM
No, that's a page I've set up specifically to show up only when you get a 404 error. It's one of the options my web host gives me. If I was given a 403 or any other error, a different page would be shown.
Yes, I've gotten others to work. However they've only been things that are pre-installed or someone on the server side set it up as was the case with my guestbook.
The fact that you get the whoops page adds to my suspicion that there might be something wrong with the cgo-bin. Last week, I went back and forth with help desk about it spontaneously reseting the permissions. :P
Nedals
02-21-2005, 09:28 PM
As a test, add this to your cgi-bin as 'test.cgi'.
Change the shebang line for your server and set the permissions to chmod 755.
#!/usr/local/bin/perl
print "Content-Type: text/html\n\n";
print "<html><head><title>Test Page</title></head>";
print "<body><h1>Hello World</h1></body></html>";
Run the script with 'http://mindsjourney.net/cgi-bin/test.cgi" from the browser location bar. It should output 'Hello World'. If that does not work, you have server problems.
penguine
02-22-2005, 12:41 AM
Okay... that one's giving me a 500.
Just to see what would happen, I took out the 404 redirect and suddenly the poll page is giving a 500. So there seems to be a bug in my webhost's error pages module since I only had it set to go to whoops.html when getting a 404. I'll file a bug report on that one tomorrow.
Errm... I did something in retyping the perl path on the hello world and now it's working. strange.
I've checked the perl path on the poll.cgi code.. and it's right so.. why isn't it working?
Here's a text version of code...
http://mindsjourney.net/Poll/pollcode.txt
It's exactly how Joe has it on the HTMLgoodies site.. except I fixed the perl path to how it is on my server... I altered the pathes of the graphics... and I altered the names of answer three to narf and Narf.
Nedals
02-22-2005, 01:15 AM
Let me just make sure I understood what you just said..
I did something in retyping the perl path and now it's working.
I assume you mean that script I posted.
I've checked the perl path on the poll.cgi code.. and it's right so.. why isn't it working?
There could be any number of reasons. Try adding the following at the top of your code, leaving out that 404 redirect.
#!/usr/local/bin/perl (your version here)
use CGI;
use CGI::Carp(qw/fatalsToBrowser/);
...
and the rest of your script
Errors will now be reported on your browser giving the cause and an indication of the line number that contains the error. One note here: sometimes the error may have occured in an earlier line. (like if you missed a closing '}' )
penguine
02-22-2005, 11:08 AM
Software error:
syntax error at poll.cgi line 102, near ""<IMG SRC="http"
syntax error at poll.cgi line 123, near ""<IMG SRC="http"
syntax error at poll.cgi line 143, near ""<IMG SRC="http"
Execution of poll.cgi aborted due to compilation errors.
There's what it tells me.
Nedals
02-22-2005, 11:58 AM
This...
print "<IMG SRC="http://mindsjourney.net/Poll/pollred.gif" height=7 width=";
To... (nested quote problem, missing width, missing '>')
print '<IMG SRC="http://mindsjourney.net/Poll/pollred.gif" height=7 width=12>';
Repeat for the other lines.
penguine
02-22-2005, 01:35 PM
The > isn't missing. It's down several lines at the end of the ALT.
print "<IMG SRC="http://mindsjourney.net/Poll/pollblue.gif" height=7 width=";
print $line1_answers[1] / ($line1_answers[0] + $line1_answers[1] + $line1_answers[2])*100;
print " ALT=No answers>";
But let me fix those other things and see what happens.
Okay.. now the page is executing... however.. it's printing the code to the answer images instead of the images themselves.
And is there a way to display the results without voting?
penguine
02-22-2005, 02:09 PM
nm about the displaying results part. figured that one out on my own.
Nedals
02-22-2005, 06:34 PM
I noticed a few problems with that script you plan to use.
1. If there are no results you will get a divide by zero error.
2. If all results are equal the result will read 3.3333333333333....
3. Image widths should be integers.
4. Users accessing the script similtaneously will cause erroneous results.
Here's a commented rewrite for you to review with some tpis for writing .cgi scripts in the future.
This is only tested for syntax but should work for you, if you so choose.
I've commented out the file lock because it does not work on older versions of Perl.
If you copy and paste this, MAKE SURE the that last line 'HTML_OUTPUT' does NOT contain a space at the end.
#!/usr/bin/perl
use strict; ## ALWAYS use strict
use CGI; ## Use this module (comes with the Perl distrubution) to get 'post' and 'get' data
my $q = new CGI;
## If the file does not exist, then create it with a 0,0,0 result
unless (-e "poll.dat") {
open(POLL, ">poll.dat") || die "Cannot create poll.dat: $!";
print POLL "0\t0\t0\n";
close(POLL);
}
## Open (and lock) the file for read and write
open(POLL, "+<poll.dat") || die "Cannot open poll.dat: $!";
#flock(POLL, 2) || die "Cannot lock poll.dat: $!"; ## not supported on all systems.
my $poll_data = <POLL>; ## only reading one line here!
chomp $poll_data;
## Seperate the results into an array
my @poll_results = split(/\t/, $poll_data);
## Using CGI, get the results from the latest poll response and add to current results.
if ($q->param('QUESTION') eq "yes") { $poll_results[0]++; }
elsif ($q->param('QUESTION') eq "no") { $poll_results[1]++; }
elsif ($q->param('QUESTION') eq "narf") { $poll_results[2]++; }
## Save the updated results.
print POLL "$poll_results[0]\t$poll_results[1]\t$poll_results[2]\n";
close(POLL);
## Declare a set of default results
my @yes_result = (0,1);
my @no_result = (0,1);
my @maybe_result = (0,1);
my $total_answers = $poll_results[0] + $poll_results[1] + $poll_results[2];
## Do not allow a divide by zero
if ($total_answers > 0) {
if ($poll_results[0] > 0) { ## Only do this if there are some results othewise use the default
## Format the precentage to xx.x%
$yes_result[0] = sprintf("%.1f", ($poll_results[0] / $total_answers)*100);
## Use an integer for the width with a minium of 1
$yes_result[1] = int($yes_result[0] + 1);
}
if ($poll_results[1] > 0) {
$no_result[0] = sprintf("%.1f", ($poll_results[1] / $total_answers)*100);
$no_result[1] = int($no_result[0] + 1);
}
if ($poll_results[2] > 0) {
$maybe_result[0] = sprintf("%.1f", ($poll_results[2] / $total_answers)*100);
$maybe_result[1] = int($maybe_result[0] + 1);
}
}
## Try to keep your Perl and HTML seperated as much as possible. Much easier to debug
## Output the HTML page using a 'here-is' construct.
print "Content-type: text/html\n\n";
print <<HTML_OUTPUT;
<HTML><HEAD><TITLE>POLL RESULTS</TITLE></HEAD>
<BODY BGCOLOR=FFFFCC>
<CENTER><H2>Poll Results</H2></CENTER>
<h3>Would You Go To The North Poll?</h3><BR>
<TABLE BORDER=1 cellpadding=5 cellspacing=1>
<TR><TD><b>Answer</b></TD><TD><b>Number</b></TD><TD><b>Percentage</b></TD><TD><b>Visual</b></TD></TR>
<TR><TD>Yes:</TD><TD>$poll_results[0]</TD><TD>$yes_result[0]%</TD>
<TD><IMG SRC="/Poll/pollred.gif" width=$yes_result[1] height=7 ALT=Yes answers></TD></TR>
<TR><TD>No:</TD><TD>$poll_results[1]</TD><TD>$no_result[0]%</TD>
<TD><IMG SRC="/Poll/pollred.gif" width=$no_result[1] height=7 ALT=No answers></TD></TR>
<TR><TD>Maybe:</TD><TD>$poll_results[2]</TD><TD>$maybe_result[0]%</TD>
<TD><IMG SRC="/Poll/pollred.gif" width=$maybe_result[1] height=7 ALT=Maybe answers></TD></TR>
<TR><TD colspan=4>Total Number of Answers: $total_answers</TD></TR>
</TABLE>
</BODY>
</HTML>
HTML_OUTPUT
QUERY :confused:
Why is there a 'return' in this line?
print POLL " $poll_results[0]\t$poll_results[1]\t$poll_results[2]\n";
penguine
02-22-2005, 07:47 PM
Like I said in the beginning, this is a code that was written by Joe burns and posted on HTMLgoodies.com. I merely followed the instructions given there. Perhaps someone needs to pass along a note to have the script fixed. I, myself, have never written any CGI. Right now, I'm just trying to get past installing the stuff. @_@
I don't know why there's a return on that line.
I'll toy with your script and install it, then yack back with the results.
penguine
02-22-2005, 08:05 PM
Okay... I tried the script you put together. I pasted it as is and it gave a 500. I took out the return that you said shouldn't be there and it still gives a 500.
Nedals
02-22-2005, 08:58 PM
Darn!
I forgot to leave in this line after use GGI
use CGI::Carp(qw/fatalsToBrowser/);
Add it in, and the problem should become apparant.
I don't know why there's a return on that line.
This was directed at others on the forum that may have an answer. Did you take it out? I hope.
print POLL "$poll_results[0] .... 2]\n"; should be on one line.
penguine
02-22-2005, 10:01 PM
As I said in my previous post, yes I took the return out.
I added that line to your code to try and get the error read out, but it didn't work. :S I just got the same 500 error.
Nedals
02-23-2005, 12:07 AM
Very odd!
Is this line correct for your server?
#!/usr/bin/perl
penguine
02-23-2005, 12:54 AM
Yep. But you can close this case. I found a php poll that actually works the way it's suppose to the first time around.