/* ****************************
* Simple PHP Poll Script
* by Jeff McGlynn
* November 2004
******************************/
error_reporting(0);
$here = dirname(__FILE__) . "/";
$pfile = "poll.dat";
$today = date("Ymd");
## Compensate for PHP 4x
if (!function_exists("file_get_contents")) {
function file_get_contents ($file) {
return implode("", file($file));
}
}
if (count($_POST)) {
## First check to see if vote counts ...
session_start();
if (strpos($_COOKIE["shs_voteKey"], "-")) {
$keyArea = explode ("-", $_COOKIE["shs_voteKey"]);
$dateArea = $keyArea[1];
$keyArea = $keyArea[0];
} else {
$keyArea = $_COOKIE["shs_voteKey"];
$dateArea = "";
}
if (!array_key_exists("shs_voteKey", $_COOKIE)) {
err("Please enable cookies to vote.");
} elseif ($dateArea == $today) {
err("You have already voted today.");
} elseif ($keyArea != $_POST["key"]) {
err("An unknown error has occurred. Please try again.");
} elseif (is_file($pfile . ".adminlock")) {
err("The poll is currently being maintained. Please go back and try again.");
} else {
if (!is_numeric($_POST["for"])) error("Invalid vote.");
else {
## Lock file
touch($pfile . ".lock");
## Load the actual poll, do some checking
$poll = unserialize(file_get_contents($pfile));
if ($poll["uniqid"] != $keyArea) {
unlink($pfile . ".lock");
err ("The poll has changed. Please go back and try again.");
} elseif (array_key_exists($_POST["for"], $poll["optNum"])) {
## Register vote
$poll["optNum"][$_POST["for"]] += 1;
$f = fopen($pfile, "w");
fputs ($f, serialize($poll));
fclose ($f);
unlink($pfile . ".lock");
setcookie("shs_voteKey", $poll["uniqid"] . "-" . $today, (time() +(24*60*60)));
} else {
unlink($pfile . ".lock");
err("Invalid option. Please try again.");
}
}
}
}
disp();
function disp ($error = "") {
global $poll, $self, $pfile, $today;
if (!defined($poll)) $poll = unserialize(file_get_contents($pfile));
?>
The Sammy Poll
echo "" . $poll["title"] . "\n\n";
if ((array_key_exists("results", $_GET) || count($_POST)) || $error != "") {
echo "" . $error . "";
## Display poll results
echo "\n";
$total = array_sum($poll["optNum"]);
$max = 0;
$total = 0;
foreach ($poll["optNum"] as $o) {
if ($o > $max) $max = $o;
$total += $o;
}
foreach ($poll["opt"] as $id => $option) {
if ($option == "") continue;
$per = round(($poll["optNum"][$id] / $total) * 100);
$x = round(($poll["optNum"][$id] / $max) * 100);
echo "| \n";
echo "$per% | $option — ";
echo $poll["optNum"][$id];
echo " | \n";
echo " \n";
echo "";
echo "";
echo " | ";
echo " | ";
echo " ";
echo " | \n";
}
echo " \n";
echo "— " .$total . " total votes. —";
} else {
if (substr($_COOKIE["shs_voteKey"], (-1 * $today)) != $today) setcookie("shs_voteKey", $poll["uniqid"], (time() +(24*60*60)));
echo '";
}
exit;
}
function err ($error) {
disp($error);
}
?> |