wikedawsum Posted January 24, 2008 Share Posted January 24, 2008 Hello all. I have a script that I found that allows me to use a simple polling system on my website. The poll works great, except that it does not prevent people from voting more than once. I was wondering if there would be a way to add an ip address check into this script to prevent multiple voting? Script for poll: <?php $pollQuestion = ''; $answers = ''; function readData(){ global $pollQuestion,$answers; // Read configuration $rawdata = file('polldata.txt'); // Get the question for polling $pollQuestion = $rawdata[0]; // Get number of answers - The foirs row is the question $numberOfAnswers = sizeof($rawdata)-1; $count = 0; for ($i=1; $i <= $numberOfAnswers; $i++){ $answerData = explode(':',$rawdata[$i]); // If tha actual row is not empty than add to the answers array if (strlen(trim($answerData[0]))>0){ $answers[$count]['text'] = $answerData[0]; $answers[$count]['count'] = $answerData[1]; ++$count; } } } function writeData(){ global $pollQuestion,$answers; $file = fopen('polldata.txt','w'); fwrite($file,$pollQuestion."\r\n",strlen($pollQuestion)); foreach ($answers as $value) { $row = $value['text'].':'.$value['count']."\r\n"; fwrite($file,$row,strlen($row)); } fclose($file); } readData(); ?> <div id="main"> <?php if (!isset($_POST['submitBtn'])) { ?> <div class="caption"><?php echo $pollQuestion; ?></div> <form class="poll" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="poll"> <table width="200"> <?php foreach ($answers as $value) { echo '<tr><td><input type="radio" name="polling" value="' .$value['text'].'"/> '.$value['text'].'</td></tr>'; } ?> <tr><td align="center"><br/> <input class="text" type="submit" name="submitBtn" value="Vote" /> </td></tr> </table> </form> <?php } else { $count = 0; foreach ($answers as $value) { if ($value['text'] == $_POST['polling']) { $answers[$count]['count'] = ((int)$value['count'])+1; (int)$totalCount++; } ++$count; } writeData(); ?> <div class="caption"><?php echo $pollQuestion; ?></div> <div id="result"> <?php echo ' <table width="200">'; foreach ($answers as $value) { echo '<tr><td> '.$value['text'].'</td><td>'.$value['count'].'</td></tr>'; } echo ' </table>'; } ?> </div> <div class="caption">Thanks for your vote!</div> </div> I know there are ways to check the ip address.. I'm just not sure how to add it to this code so that it will work correctly. Any suggestions or help is greatly appreciated. Thanks, wikedawsum Quote Link to comment Share on other sites More sharing options...
irvieto Posted January 26, 2008 Share Posted January 26, 2008 hello. Do you have access to a database or make one? If so, the problem is solved because you can create a function to check if the user IP is already logged. If there is no database access, is more complex. Maybe registering the user IP into a second *.txt file and again create a function to check if the IP is in its content. Database way is VERY recommended in my opinnion. Quote Link to comment Share on other sites More sharing options...
revraz Posted January 26, 2008 Share Posted January 26, 2008 And what if I change IPs, I can vote again? Mark them as voting in a Database. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.