Code: Select all
<?
ob_start();
//show voting options
function showOptions(){
$file = "poll.txt";
print "<form action='index.php' method='post'>";
$open = fopen($file, "r+");
while (!feof($open)){
$read = fgetss($open, 99999);
$read = explode(",", $read);
$count = count($read);
for ($i=0; $i<$count; $i++){
if($i == "0"){
print "<input type='radio' name='option' value='" . $i . "'> " . $readї$i] . "<br>";
}else
print "<input type='radio' name='option' value='" . $i . "'>" . $readї$i] . "<br>";
}
}
fclose($open);
print "<input type='submit' value='submit' name='submit'>";
print "</form>";
print '<br>';
}
//end
//add votes to data.txt and add ip to ip.txt
if (isset($_POSTї'option']) && isset($_POSTї'submit'])){
addVotes();
}
//function addVotes
function addVotes(){
checkIP();
$option = $_POSTї'option'];
function getOrder($n){
return intval(trim($n));
}
$array = array_map('getOrder', explode(',', file_get_contents("data.txt")));
$arrayї$option]++;
$fp = fopen('data.txt', 'r+');
fwrite($fp, implode(',',$array));
fclose($fp);
$fz = fopen('ip.txt', 'a');
$ip = $_SERVERї'REMOTE_ADDR'] . ',';
fwrite($fz, $ip);
fclose($fz);
header('location: index.php?id=1');
}
//end function
if($id == "showResults"){
$array1 = explode(',', file_get_contents("poll.txt"));
$array2 = explode(',', file_get_contents("data.txt"));
$count = count($array1);
for ($i=0; $i<$count; $i++){
$sum = $sum + $array2ї$i];
}
for ($b=0; $b<$count; $b++){
print $array1ї$b];
echo "<font size='2'> has ";
print $array2ї$b]; echo ($array2ї$b] == 1 ? ' vote' : ' votes') . "</font><br>";
print "<img src='bar.jpg' height='20' width='" . ($array2ї$b]/$sum)*150 . "'><br>";
}
}
//check to see if the ip has already voted
function checkIP(){
$arrIP = explode(",", file_get_contents("ip.txt"));
$count = count($arrIP);
for ($i=0; $i<$count; $i++){
if ($arrIPї$i] == $_SERVERї'REMOTE_ADDR']){
header('location: http://www.wesmokerocks.com/shizPoll/index.php?id=showResults');
exit();
}
}
//if it past the ip test
showOptions();
}
if (!isset($id)){
checkIP();
}
?>