This site is developed to XHTML and CSS2 W3C standards.
If you see this paragraph, your browser does not support those standards and you
need to upgrade. Visit WaSP
for a variety of options.
Paste #656
Posted by: gpoll
Posted on: 2026-03-10 22:17:40
Age: 1 day ago
Views: 21
<?php
session_start();
$data_dir="data";
$users_file="$data_dir/users.txt";
$polls_file="$data_dir/polls.txt";
$votes_dir="$data_dir/votes";
if(!file_exists($data_dir)) mkdir($data_dir);
if(!file_exists($votes_dir)) mkdir($votes_dir);
if(!file_exists($users_file)) file_put_contents($users_file,"");
if(!file_exists($polls_file)) file_put_contents($polls_file,"");
$a=isset($_GET["a"])?$_GET["a"]:"";
function head($title){
echo "<html><head><title>$title</title></head><body>";
echo "<table border=1 width=700 align=center><tr><td>";
}
function foot(){
echo "</td></tr></table></body></html>";
}
function check_login($u,$p){
$lines=file("data/users.txt");
foreach($lines as $l){
list($lu,$lp)=explode("|",trim($l));
if($u==$lu && $p==$lp) return 1;
}
return 0;
}
function user_exists($u){
$lines=file("data/users.txt");
foreach($lines as $l){
list($lu,$lp)=explode("|",trim($l));
if($u==$lu) return 1;
}
return 0;
}
function get_polls(){
return file("data/polls.txt");
}
function save_poll($title,$opts){
$id=time();
$str=$id."|".$title."|".implode(",",$opts)."\n";
file_put_contents("data/polls.txt",$str,FILE_APPEND);
}
function vote($poll,$opt){
$file="data/votes/".$poll.".txt";
$data=array();
if(file_exists($file)){
$lines=file($file);
foreach($lines as $l){
list($o,$c)=explode("|",$l);
$data[$o]=$c;
}
}
if(!isset($data[$opt])) $data[$opt]=0;
$data[$opt]++;
$f=fopen($file,"w");
foreach($data as $o=>$c){
fwrite($f,$o."|".$c."\n");
}
fclose($f);
}
if($a=="register" && $_POST){
$u=$_POST["user"];
$p=$_POST["pass"];
if(!user_exists($u)){
file_put_contents("data/users.txt",$u."|".$p."\n",FILE_APPEND);
$msg="Registered. Now login.";
}else{
$msg="User exists";
}
}
if($a=="login" && $_POST){
if(check_login($_POST["user"],$_POST["pass"])){
$_SESSION["user"]=$_POST["user"];
header("Location: index.php");
exit;
}else{
$msg="Wrong login";
}
}
if($a=="logout"){
session_destroy();
header("Location: index.php");
exit;
}
if($a=="create" && $_POST){
if($_SESSION["user"]){
$title=$_POST["title"];
$opts=array(
$_POST["o1"],
$_POST["o2"],
$_POST["o3"],
$_POST["o4"]
);
save_poll($title,$opts);
header("Location: index.php");
exit;
}
}
if($a=="vote" && $_POST){
vote($_GET["id"],$_POST["option"]);
head("Vote");
echo "Thanks for voting";
foot();
exit;
}
if($a=="vote"){
$id=$_GET["id"];
$polls=get_polls();
foreach($polls as $p){
list($pid,$title,$opts)=explode("|",$p);
if($pid==$id){
$options=explode(",",$opts);
}
}
head("Vote");
echo "<b>$title</b><br><br>";
echo "<form method=post>";
foreach($options as $o){
echo "<input type=radio name=option value='$o'> $o<br>";
}
echo "<br><input type=submit value=Vote>";
echo "</form>";
foot();
exit;
}
if($a=="results"){
$id=$_GET["id"];
head("Results");
$file="data/votes/".$id.".txt";
if(file_exists($file)){
echo "<table border=1 width=400>";
$lines=file($file);
foreach($lines as $l){
list($o,$c)=explode("|",$l);
echo "<tr><td>$o</td><td>$c</td></tr>";
}
echo "</table>";
}else{
echo "No votes yet";
}
foot();
exit;
}
if($a=="create"){
if(!$_SESSION["user"]){
header("Location: index.php");
exit;
}
head("Create poll");
echo "<form method=post>";
echo "Title<br>";
echo "<input name=title><br><br>";
echo "Option 1<br>";
echo "<input name=o1><br>";
echo "Option 2<br>";
echo "<input name=o2><br>";
echo "Option 3<br>";
echo "<input name=o3><br>";
echo "Option 4<br>";
echo "<input name=o4><br><br>";
echo "<input type=submit value='Create'>";
echo "</form>";
foot();
exit;
}
head("Polls");
if($_SESSION["user"]){
echo "Logged as ".$_SESSION["user"]." | ";
echo "<a href=?a=create>Create poll</a> | ";
echo "<a href=?a=logout>Logout</a>";
}else{
echo "<table width=100%><tr><td width=50%>";
echo "<b>Login</b><br>";
echo "<form method=post action=?a=login>";
echo "User<br><input name=user><br>";
echo "Pass<br><input type=password name=pass><br>";
echo "<input type=submit value=Login>";
echo "</form>";
echo "</td><td>";
echo "<b>Register</b><br>";
echo "<form method=post action=?a=register>";
echo "User<br><input name=user><br>";
echo "Pass<br><input type=password name=pass><br>";
echo "<input type=submit value=Register>";
echo "</form>";
echo "</td></tr></table>";
}
if(isset($msg)) echo "<br>$msg<br>";
echo "<hr>";
$polls=get_polls();
echo "<table border=1 width=600>";
echo "<tr><td>Poll</td><td>Links</td></tr>";
foreach($polls as $p){
list($id,$title,$opts)=explode("|",$p);
echo "<tr>";
echo "<td>$title</td>";
echo "<td>";
echo "<a href=?a=vote&id=$id>Vote</a> ";
echo "<a href=?a=results&id=$id>Results</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
foot();
?>
Download raw |
Create new paste