"; echo "
"; } function foot(){ echo "
"; } 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 "$title

"; echo "
"; foreach($options as $o){ echo " $o
"; } echo "
"; echo "
"; foot(); exit; } if($a=="results"){ $id=$_GET["id"]; head("Results"); $file="data/votes/".$id.".txt"; if(file_exists($file)){ echo ""; $lines=file($file); foreach($lines as $l){ list($o,$c)=explode("|",$l); echo ""; } echo "
$o$c
"; }else{ echo "No votes yet"; } foot(); exit; } if($a=="create"){ if(!$_SESSION["user"]){ header("Location: index.php"); exit; } head("Create poll"); echo "
"; echo "Title
"; echo "

"; echo "Option 1
"; echo "
"; echo "Option 2
"; echo "
"; echo "Option 3
"; echo "
"; echo "Option 4
"; echo "

"; echo ""; echo "
"; foot(); exit; } head("Polls"); if($_SESSION["user"]){ echo "Logged as ".$_SESSION["user"]." | "; echo "Create poll | "; echo "Logout"; }else{ echo "
"; echo "Login
"; echo "
"; echo "User

"; echo "Pass

"; echo ""; echo "
"; echo "
"; echo "Register
"; echo "
"; echo "User

"; echo "Pass

"; echo ""; echo "
"; echo "
"; } if(isset($msg)) echo "
$msg
"; echo "
"; $polls=get_polls(); echo ""; echo ""; foreach($polls as $p){ list($id,$title,$opts)=explode("|",$p); echo ""; echo ""; echo ""; echo ""; } echo "
PollLinks
$title"; echo "Vote "; echo "Results"; echo "
"; foot(); ?>