douxidang9092 2014-04-16 17:20
浏览 14
已采纳

使用PHP增加Cookie的价值

I'm just looking to create a cookie which stores the number of correct answers someone gives in a quiz and then increment it for every correct answer.

if(isset($_GET['first'])){setcookie("answers", 0, time()+900);}
elseif($_POST['1']){
    if($_POST['1']=='y'){
        $_COOKIE['answers']++;
    }

The first part just sets the cookie initially if they are starting the quiz. After they answer, they will move to the next page (which is the same page but with different HTML) which also has a question named "1".

The problem is, the cookie keeps getting reset to 0 at the start of every page. However, I'm positive I'm not setting it again. Any help would be much appreciated! Also, I know this isn't a very robust way to create a quiz but it's an exercise.

  • 写回答

2条回答 默认 最新

  • douyi0219 2014-04-16 17:40
    关注
    <?php
    if(!isset($_COOKIE['answers']) || isset($_GET['first'])) {
     setcookie("answers",0,time() + 900);
    } else if (isset($_POST['1']) && $_POST['1'] === 'y') {
     setcookie("answers", $_COOKIE['answers'] + 1 , time() + 900);
    }
    

    NOTE : each time you change the cookie value its expiration is also changed

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度