javascript - 创建 Cookie 以不再在民意调查中投票

标签 javascript php jquery ajax cookies

我使用 jQuery AJAX 和 JSON 创建了一个简单的[投票表单]。我想知道如何创建 Cookie,以便用户不能多次投票。以下是我的代码。

我是 Cookie 和 jQuery 的新手。请告诉我如何完成我的任务。

JavaScript

<script>
$(document).ready(function(){

    $("#poll").click(function(){
        var count = '';
        if (document.getElementById("vote1").checked) {
            count = 0;
        }
        if (document.getElementById("vote2").checked) {
            count = 1;
        }
        var jsonV= { "vote": count };

        $.ajax({
          type  : "POST",
          url   : "poll_vote.php",
          data  : jsonV,
          dataType: "json",
              success : function ( responseText ){
                console.log("Is working " + responseText);
                 $("#result").html( responseText.vote );
              },
              complete : function(){
                  $("#poll").slideUp();
              },
              error : function( error,responseText ){
                // alert("Server not Responding. Sorry for the inconvenience caused. Please Try again Later");
                console.log( error );
                $("#result").html( error + responseText );
                alert( count );
              }
        });
    });
});
</script>

PHP

<?php

$vote = $_REQUEST['vote'];
$filename = "poll_result.txt";
$content = file($filename);
// $decode = json_decode($encode);
$array = explode("||", $content[0]);
$male = $array[0];
$female = $array[1];

if ($vote == '0') {
  $male = $male + 1;
}
if ($vote == '1') {
  $female = $female + 1;
}

$insertvote = $male."||".$female;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);

$table =  (
    "<h2>Results:</h2>
        <table>
        <tr>
            <td> Male :</td>
            <td>
            <img src='poll.gif'
            width= ".(100*round($male/($female+$male),2)).
            "height='20'>".
            (100*round($male/($female+$male),2))." %"  .
            "
             </td>
         </tr>
         <tr>
             <td> Female :</td>
             <td>
             <img src='poll.gif'
             width=". (100*round($female/($female+$male),2)) .
            "
             height='20'>".
             (100*round($female/($female+$male),2))." %" ."

             </td>
         </tr>
     </table>"
 );
$list  = array('vote' => $table);
$encode = json_encode($list);
echo $encode;
?>

HTML

<div id= "poll">
    <h3> What is your Gender? </h3>
    <form>
        Male : 
        <input type ="radio" name ="vote"  id="vote1" >
        <br>
        Female :
        <input type ="radio" name ="vote"  id="vote2" >
    </form>
</div>

最佳答案

您可能希望在用户投票时设置一个 cookie,并在提交投票时在 PHP 中检查该 cookie。如果 cookie 已设置,则应放弃投票。

例如,仅使用 PHP,它可能看起来像这样:

if (!isset($_COOKIE['has_voted'])) {
    // normal vote submission code goes here
    // ...
    // then we set a cookie to expire in 30 days
    setcookie('has_voted', '1', mktime().time()+60*60*24*30);
} else {
    // cookie already exists, user has already voted on this machine
    // do not count the vote, flag an error to the user
}

值得注意的是,有一些方法可以解决这个问题 - 用户可以轻松地手动删除 cookie。在这种情况下,您还可以存储已投票用户的 IP 地址,但这可能会在共享计算机和网络后面的多台计算机上出现问题。

关于javascript - 创建 Cookie 以不再在民意调查中投票,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29717023/

相关文章:

javascript - 在 JavaScript 中输入时删除 < > 符号

javascript - Wordpress,以正确的方式添加自定义脚本

php - SQL 计算具有相同 sub_id 的两个值的百分比。

php - 将 WHERE 函数与 $_POST 一起使用

java - 撇号 chop 输入字段中的值

javascript - 如何让这些按钮正常工作

javascript - HTML 此声明

php - Composer undefined variable : auth

javascript - 使用 Javascript 和经典 ASP 的 PayPal 基础知识

jquery - 我可以在 sprite 图像上叠加文本吗?