Quote:
|
Originally Posted by David43
Could you suggest any CGI scrip in particular?
|
Huh... It is so easy to write such poll script... Here we go
Here is the script.
File poll.cgi
Code:
#!/usr/bin/perl
use strict;
use CGI qw(:all);
my $poll_id = int(param('poll'));
my $action;
open FH, "poll_${poll_id}_ip.txt";
my $ip = <FH>;
close FH;
chomp $ip;
$ip = defined($ip) ? $ip : '';
if ($ip eq '') { $ip = ':' }
my $pat = join('\.', split(/\./, $ENV{'REMOTE_ADDR'}));
my $mode = $ip =~ /\:$pat\:/ ? 0 : 1;
if (($mode == 0) || defined(param('see'))) {
print "Content-Type: text/html\n\n";
print "<center>Poll results</center>\n";
open FH, "poll_${poll_id}_text.txt";
my @lines = <FH>;
close FH;
print join("<br>", @lines);
open FH, "poll_${poll_id}_lines.txt";
my @lines = <FH>;
close FH;
open FH, "poll_${poll_id}_results.txt";
my @votes = <FH>;
close FH;
my $sum = 0;
foreach (@votes) {
$sum += int($_);
}
print "<center><table border=0 cellspacing=0 cellpadding=4>";
for (my $i = 0; $i <= $#lines; $i++) {
my $width = 200;
my $a = int($votes[$i] * $width / $sum);
my $b = $width - $a;
print "<tr><td align=right>$lines[$i] (".int($votes[$i]).")</td><td><table width=$width height=10><tr><td width=$a bgcolor=red></td><td width=$b bgcoloe=blue></td></tr></table></td></tr>";
}
print "</table></center>";
} else {
if (defined(param('dovote'))) {
my $vote = int(param('vote'));
open FH, "poll_${poll_id}_lines.txt";
my @lines = <FH>;
close FH;
if ($vote > $#lines) {
print "Content-Type: text/html\n\nError";
exit 0;
}
open FH, "poll_${poll_id}_results.txt";
my @lines = <FH>;
close FH;
foreach (@lines) {
chomp $_;
}
$lines[$vote] = int($lines[$vote]) + 1;
open FH, ">poll_${poll_id}_results.txt";
print FH join("\n", @lines);
close FH;
open FH, ">poll_${poll_id}_ip.txt";
print FH "$ip$ENV{'REMOTE_ADDR'}:";
close FH;
print redirect(-uri => $ENV{'HTTP_REFERER'});
} else {
print "Content-Type: text/html\n\n";
print "<form action=/cgi-bin/poll.cgi method=post>";
print "<input type=hidden name=poll value=$poll_id>";
open FH, "poll_${poll_id}_text.txt";
my @lines = <FH>;
close FH;
print join("<br>", @lines);
print "<center><table border=0 cellspacing=0 cellpadding=4>";
open FH, "poll_${poll_id}_lines.txt";
my @lines = <FH>;
close FH;
for (my $i = 0; $i <= $#lines; $i++) {
print "<tr><td align=center><input type=radio name=vote value=$i></td><td>$lines[$i]</td></tr>";
}
print "<tr><td align=center colspan=2><input type=submit name=dovote value=Vote> <input type=submit name=see value=\"See results\"></td></tr>";
print "</table></center>";
print "</form>";
}
}
Requirements: http server must have write access to config files.
Configuration:
To create poll
1. Create file "poll_ X_text.txt" (replace X with integer poll identifier e.g. "1") and put there poll question.
2. Create file "poll_ X_lines.txt" with referred answers (1 answer per line)
3. Put on your page SSI call: <!--#include virtual="/cgi-bin/poll.cgi?poll= X"-->
Poll rules: a client can vote once. After he votes, his IP is stored and script shows him results page.
Example
poll_1_text.txt
poll_1_lines.txt
Code:
Yes.
No.
May be.
Do I like what???
__________________
Software outsourcing / Free bulletin board, hit counter and banner exchange services
Please login or register to view this content. Registration is FREE
|