【问题标题】:Code for Poll with unique voters具有唯一选民的投票代码
【发布时间】:2014-05-25 00:27:49
【问题描述】:

我有以下 PHP/AJAX 代码用于 w3schools.com 提供的投票:) 我想让用户只投票一次(每天或每 24 小时投票一次会很好),有什么想法吗?

Javascript 部分

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

<body>
<div id="poll">
<h3>First Question?</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>
</body>
</html> 

PHP部分

<?php
$vote = $_REQUEST['vote'];

$filename = "poll_result.txt";
$content = file($filename);

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

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

$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>

谢谢!

【问题讨论】:

  • 您能否更具体地说明您尝试过的方法不适合您?
  • 可能也想将数据插入数据库。您可以使用 PHP 的 time() 函数,将其发送到您的数据库,然后,当用户返回到您再次运行 time() 的页面时,确保存在超过 86400000 毫秒的差异。或者使用$_SESSION,但可以由用户删除。
  • 要检测唯一性,您可以使用 cookie(可以按照 PHPglue 的指示将其删除),或者您可以使用它们的公共 IP。使用 IP 的问题是图书馆或学校等公共机构只能进行一次投票。一种方法是允许输入一个使用访问代码作为身份验证来进行投票(这似乎有点矫枉过正,因为这只是一个投票,而不是调查)。
  • 谢谢,您的回复真的很有帮助!

标签: javascript php jquery ajax unique-index


【解决方案1】:

你可以试试这个。

<script>
function setCookie(cname,cvalue,exdays)
{
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++)
  {
  var c = ca[i].trim();
  if (c.indexOf(name)==0) return c.substring(name.length,c.length);
}
return "";
}


function getVote(int) {
  if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest();
  } else {  
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      setCookie("voted",1,1)
      document.getElementById("poll").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","poll_vote.php?vote="+int,true);
  if(!getCookie("voted")){
    xmlhttp.send();
    }
    else{
    alert("you already voted!")
    }
}
</script>

还有第二种变体是将投票的 IP 存储在数据库或文件中。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-07-14
  • 1970-01-01
  • 2010-09-09
  • 2012-07-11
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
  • 2017-05-07
相关资源
最近更新 更多