Javascript 具有唯一投票人的投票代码
我有以下PHP/AJAX代码用于w3schools.com提供的民意测验:) 我想让用户只投票一次(每天或每24小时投票一次就好了),你有什么想法 Javascript部分Javascript 具有唯一投票人的投票代码,javascript,php,jquery,ajax,unique-index,Javascript,Php,Jquery,Ajax,Unique Index,我有以下PHP/AJAX代码用于w3schools.com提供的民意测验:) 我想让用户只投票一次(每天或每24小时投票一次就好了),你有什么想法 Javascript部分 函数getVote(int){ if(window.XMLHttpRequest){ xmlhttp=新的XMLHttpRequest(); }否则{ xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”); } xmlhttp.onreadystatechange=函数(){ if(xm
函数getVote(int){
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}否则{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“poll”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”、“poll_vote.php?vote=“+int,true”);
xmlhttp.send();
}
第一个问题?
对:
不:
PHP部分
结果:
对:
'
高度class='20'>
%
不:
'
高度class='20'>
%
谢谢 你可以试试这个
<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>
函数setCookie(cname、cvalue、exdays)
{
var d=新日期();
d、 设置时间(d.getTime()+(exdays*24*60*60*1000));
var expires=“expires=“+d.togmString();
document.cookie=cname+“=”+cvalue+”;“+expires;
}
函数getCookie(cname)
{
变量名称=cname+“=”;
var ca=document.cookie.split(“;”);
对于(var i=0;我能更具体地说明您尝试过的不适用于您的内容吗?可能也想将数据插入数据库。您可以使用PHP的time()
函数,将其发送到您的数据库,然后,当用户返回到您运行的time()页面时
再次确认,确保差异大于86400000毫秒。或者使用$\u会话,但该会话可以由用户删除。要检测唯一性,可以使用cookie(如PHPglue所述,可以删除),或者您可以使用他们的公共IP。使用IP的问题是,图书馆或学校等公共机构只能进行一次投票。一种方法是允许输入一次性访问代码作为身份验证来进行投票(这似乎有些过分,因为这只是一次投票,而不是一次调查)。谢谢,你的回复真的很有帮助!此外,我正在尝试使用以下代码添加一个“查看结果”链接:但它看起来仍然受到getVote函数的影响,该函数对投票进行计数和求和。有什么想法吗?更改函数名,如getResults(this.value),并在其中实现基本功能。
<?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>
<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>