AJAX poll with ip logging

The following script is a simple poll. But when a user with the same ip adress see the poll again after voting he will see only the results of the poll. This script uses no mysql db everthing is stored into 2 files:

  • poll_result.txt – For logging the results of the poll
  • ip.db – For logging the IP addresses after voting

Example:

Host this script for free:

Web hosting

index.php:

<?php
$ipdb = "http://your.domain.com/ip.db"; //url to the file "ip.db".
$ip = $_SERVER['REMOTE_ADDR'];
    $sitename = htmlspecialchars($_POST['name']);
    $headers = @get_headers($ipdb);
    if(strpos($headers[0],'200')===false) {
        echo "Oops the url isn't working!"; 
    } else {
        $ch = curl_init($ipdb);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $text = curl_exec($ch);
        $test = strpos($text, $ip);
        if ($test==false) {
?>			
			<script>
			function getVote(int) {
				if (window.XMLHttpRequest) {
					xmlhttp=new XMLHttpRequest();
					} else {
						xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
					}
				xmlhttp.onreadystatechange=function() {
					if (this.readyState==4 && this.status==200) {
						document.getElementById("poll").innerHTML=this.responseText;
					}
				}
			xmlhttp.open("GET","poll_vote.php?vote="+int,true);
			xmlhttp.send();
			}
			</script>

			<div id="poll">
			<h3>Do you like this site so far?</h3>
			<form>
			Yes:
			<input type="radio" name="vote" value="0" onclick="getVote(this.value)">
			<br>No:
			<input type="radio" name="vote" value="1" onclick="getVote(this.value)">
			</form>
			</div>		
			<?php		

        } else {
?>
		<script>
		if (window.XMLHttpRequest) {
			xmlhttp=new XMLHttpRequest();
		} else { 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function() {
			if (this.readyState==4 && this.status==200) {
				document.getElementById("poll").innerHTML=this.responseText;
			}
		}
		xmlhttp.open("GET","poll_vote.php",true);
		xmlhttp.send();
		</script>	
		<div id="poll">	
<?php		
		}
	}
?>

poll_vote.php:

<?php
$ipdb = 'ip.db'; //if you want to change the filename of the ip.db you can do it here.
$vote = $_REQUEST['vote'];

if (($vote == "1") or ($vote == "0")) {
	$ip = $_SERVER['REMOTE_ADDR'];
	$handle = fopen($ipdb, 'a') or die('Cannot open file:  '.$ipdb);
	$data = "\n".$ip;
	fwrite($handle, $data);
}

//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

if ($vote == 0) {
  $yes = $yes + 1;
}
if ($vote == 1) {
  $no = $no + 1;
}

//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>

<h2>Result:</h2>
<table>
	<tr>
		<td>Yes:</td>
		<td>
			<img src="poll.gif"
			width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
			height='20'>
			<?php echo(100*round($yes/($no+$yes),2)); ?>%
		</td>
	</tr>
	<tr>
		<td>No:</td>
		<td>
			<img src="poll.gif"
			width='<?php echo(100*round($no/($no+$yes),2)); ?>'
			height='20'>
			<?php echo(100*round($no/($no+$yes),2)); ?>%
		</td>
	</tr>
</table>

ip.db:
This file is empty!

poll_result.txt:
0||0

Download: HERE

One thought on “AJAX poll with ip logging

Leave a Reply

Your email address will not be published. Required fields are marked *