Jump to content

[SOLVED] Check IP Address for poll voting


wikedawsum

Recommended Posts

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.