Ciao a tutti, ho creato uno script PHP per un sondaggio.
Di seguito il codice dei due files che lo compongono:
index.php
Codice PHP:
<?php
include ('Poll.php'); $poll = new Poll(); $voted = 0; $pollData = $poll->getPoll(); if(isset($_POST['vote'])){ $pollVoteData = array( 'sondaggioid' => $_POST['sondaggioid'], 'pollOptions' => $_POST['options'] ); $isVoted = $poll->updateVote($pollVoteData); if($isVoted){ setcookie($_POST['sondaggioid'], 1, time()+60*60*24*365); $voted = 1; } else { $voted = 2; } }
?> <?php if(!empty($voted) && $voted === 1) { echo '<div class="alert alert-success">Hai votato con successo.</div>'; } else if(!empty($voted) && $voted === 2) { echo '<div class="alert alert-danger">Your had already voted.</div>'; } ?> <form action="" method="post" name="pollForm"> <?php foreach($pollData as $poll){ $pollOptions = explode("||||", $poll['options']);?> <?php echo $poll['question']?>
<?php for( $i = 0; $i < count($pollOptions); $i++ ) { ?>
<label> <input type="radio" name="options" value="<?php echo $i; ?>"> <?php echo $pollOptions[$i]?> </label> <?php }?>
<input type="hidden" name="sondaggioid" value="<?php echo $poll['sondaggioid']; ?>"/> <button type="submit" class="btn btn-primary btn-sm" id="vote" name="vote"> Vote</button> <?php }?> </form>
Poll.php
Codice PHP:
<?phpclass Poll{private $host = 'localhost';private $user = 'root';private $password = '';private $database = 'sondaggio';private $pollTable = 'poll';private $dbConnect = false;public function __construct(){if(!$this->dbConnect){$conn = new mysqli($this->host, $this->user, $this->password, $this->database);if($conn->connect_error){die("Error failed to connect to MySQL: " . $conn->connect_error);}else{$this->dbConnect = $conn;}}}private function getData($sqlQuery) {$result = mysqli_query($this->dbConnect, $sqlQuery);if(!$result){die('Error in query: '. mysqli_error());}$data= array();while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {$data[]=$row;}return $data;}public function getPoll(){$sqlQuery = 'SELECT sondaggioid, question, options, votes, voters FROM '.$this->pollTable;return $this->getData($sqlQuery);}public function getVotes($sondaggioid){$sqlQuery = 'SELECT votes, voters FROM '.$this->pollTable.' where sondaggioid = '.$sondaggioid;$result = mysqli_query($this->dbConnect, $sqlQuery);return mysqli_fetch_array($result, MYSQLI_ASSOC);}public function updateVote($pollVoteData) {if(!isset($pollVoteData['sondaggioid']) || isset($_COOKIE[$pollVoteData['sondaggioid']])) {return false;}$pollVoteDetails = $this->getVotes($pollVoteData['sondaggioid']);$votes = explode("||||", $pollVoteDetails['votes']);$votes[$pollVoteData['pollOptions']] += 1;implode("||||",$votes);$pollVoteDetails['voters'] += 1;$sqlQuery = "UPDATE ".$this->pollTable." set votes = '".implode("||||",$votes)."' , voters = '".$pollVoteDetails['voters']."' where sondaggioid = '".$pollVoteData['sondaggioid']."'";mysqli_query($this->dbConnect, $sqlQuery);return true;}}?>
Tutto funziona correttamente tranne il cookie che dovrebbe bloccare il secondo voto da parte dell'utente.
E' come se il cookie non venisse nemmeno impostato sul browser dell'utente, non c'è traccia.
Qualcuno sa dirmi cosa potrebbe essere?
Io ho la necessità di consentire un solo voto all'utente, perciò al primo accesso alla pagina quando invia il voto viene salvato un cookie che non consentirà un altro voto.
Grazie mille
MF