Jump to content


Photo
- - - - -

php flatfile polling booth


  • Please log in to reply
1 reply to this topic

#1 MillerTime

MillerTime

    Young Padawan

  • Members
  • Pip
  • 69 posts

Posted 31 July 2005 - 07:27 PM

Okay so you want a poll script eh? And you dont want to go through the mysql stuff? Then here's the script for you!

Lets start off by having all the files needed:

- config.php
- poll.php
- vote.php
- answer1.txt
- answer2.txt
- answer3.txt
- answer4.txt
- ip.txt

first in the config.php, lets type the following code:

<?php

/*COPYRIGHT ROBERT LEE (MILLERTIME)
DATE:JULY 31ST 2005
THIS IS MY SCRIPT!!!!*/
//config.php

$question = "What other things should we include in this site?"; // the question
$ans1 = "1.) Topsites"; //1st option you may change this by going 1.) OPTION Value
$ans2 = "2.) Link Dump"; //2nd option you may change this by going 2.) OPTION Value
$ans3 = "3.) Tutorial System"; //3rd option you may change this by going 3.) OPTION Value
$ans4 = "4.) NOTHING!!!"; //4th option you may change this by going 4.) OPTION Value
?>

all you need to know is that this is basically where you edit the question and the answers.nothing complicated

Now for poll.php:

<?php
include("config.php");
$ip = $_SERVER['REMOTE_ADDR']."\n";
$line = file("ip.txt");

if(in_array($ip,$line)){
$add1 = fopen("answer1.txt", "r");
$read1 = fread($add1, 1024);
$add2 = fopen("answer2.txt", "r");
$read2 = fread($add2, 1024);
$add3 = fopen("answer3.txt", "r");
$read3 = fread($add3, 1024);
$add4 = fopen("answer4.txt", "r");
$read4 = fread($add4, 1024);

print "<h3>$question</h3>";
$num = $read1 + $read2 + $read3 + $read4;
if($num == 0){
  $num1 = 0;
  $num2 = 0;
  $num3 = 0;
  $num4 = 0;
}else{
print "(Total Votes: $num)<br><br>";
$num1 = $read1 / $num;
$num1 = $num * 100;
$num1 = substr($num, 0, 4);
$num2 = $read2 / $num;
$num2 = $num * 100;
$num2 = substr($num, 0, 4);
$num3 = $read3 / $num;
$num3 = $num * 100;
$num3 = substr($num, 0, 4);
$num4 = $read4 / $num;
$num4 = $num * 100;
$num4 = substr($num, 0, 4);
}
print "$ans1-> $read1 votes = $num1%<br>";
print "$ans2-> $read2 votes = $num2%<br>";
print "$ans3-> $read3 votes = $num3%<br>";
print "$ans4-> $read4 votes = $num4%<br>";

fclose($add1);
fclose($add2);
fclose($add3);
fclose($add4);
}else{

print "$question";
?>
<form action="vote.php" method=post>
<?php
print "<input type=radio name='ans' value=1>$ans1<br>";
print "<input type=radio name='ans' value=2>$ans2<br>";
print "<input type=radio name='ans' value=3>$ans3<br>";
print "<input type=radio name='ans' value=4>$ans4<br>";
?><input type=submit value=vote! name=submit>
<?
}
?>

the first part just checks to see if the the ip of the user is in ip.txt. If it is, display the results. If it isnt, display the poll. Pretty easy eh? Nothing too complicated.

Now for vote.php:

Type this in vote.php:

<?php
if((isset($_POST[submit])) && (isset($_POST['ans']))){ //is there an answer
$ip = $_SERVER['REMOTE_ADDR']; //get users  ip
$line = file("ip.txt"); //put ip.txt into an array
$write_ip = $_SERVER['REMOTE_ADDR']."\n"; //get the text we are about to put into ip.txt if the ip isnt in the array

if(!in_array($ip,$line)){ //is the ip in the array? if it isnt, lets put it in there and carry on
$handle = fopen("ip.txt","a"); //open ip.txt with appending mode
$write = fwrite($handle,$write_ip); // write the ip with a new line


include("config.php");
//open all files for reading
$add1 = fopen("answer1.txt", "r");
$read1 = fread($add1, 1024);
$add2 = fopen("answer2.txt", "r");
$read2 = fread($add2, 1024);
$add3 = fopen("answer3.txt", "r");
$read3 = fread($add3, 1024);
$add4 = fopen("answer4.txt", "r");
$read4 = fread($add4, 1024);

//open the file for the answer for writing only
if ($_POST['ans'] == 1) {
$read = $read1 + 1;
$answer1 = fopen("answer1.txt", "w");
$write = fwrite($answer1, $read);
fclose($answer1);
} elseif($_POST['ans'] == 2) {
$read = $read2 + 1;
$answer2 = fopen("answer2.txt", "w");
$write = fwrite($answer2, $read);
fclose($answer2);
} elseif($_POST['ans'] == 3) {
$read = $read3 + 1;
$answer3 = fopen("answer3.txt", "w");
$write = fwrite($answer3, $read);
fclose($answer3);
} elseif($_POST['ans'] == 4) {
$read = $read4 + 1;
$answer4 = fopen("answer4.txt", "w");
$write = fwrite($answer4, $read);
fclose($answer4);
}
if($write){ //did it work?
echo 'Vote Added';
}else{ //if not then write
echo 'Vote wasnt added';
}
}
else{ //if ip is in array, then dont do anything
print 'No double Voting';

}
}

else{
echo 'you did not choose an answer';
exit;
}
?>

first part just checks if the user actually submitted an answer. If they did, carry on with submittion, if not, dont do anything.

Next part checks to see if the user's ip is in the array (like last time). So no need explaing that again.

Then it submits the vote by adding 1 to the number already in the answer*.txt

thats it for codes. Now we just have to do a few little things before actually using the script.

1) CHMODD answer1.txt , answer2.txt, answer3.txt, answer4.txt and ip.txt to 777
2)must have the files answer1.txt, answer2.txt, answer3.txt and answer4.txt in the same directory as well as poll.php, vote.php, config.php
3) dont like the tutorial? dont eat my head off..go somehwere else.

//TUTORIAL MADE BY ROBERT LEE (MILLERTIME)
//DATE: July 31 2005
//LOGO-SHACK.NET, kryptek.org, codedfx.com

hope you actually learned the logic from this tutorial instead of just copy paste

Edited by MillerTime, 31 July 2005 - 08:50 PM.


#2 brent

brent

    Young Padawan

  • Members
  • Pip
  • 72 posts
  • Location:Tennessee

Posted 01 August 2005 - 07:56 AM

hey MillerTime, I didn't know you was a member here also...hehe I noticed your thread on KrypTek.org good tut :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users