dongtui2029 2017-10-17 03:54 采纳率: 0%
浏览 45

Cookie没有以正确的方式设置

I have created a cookie which get set for the first time with the value 24. What I want is that every time a user comes back to the page, the cookie value should decrease. And actually that works really well if you load the page multiple times and DON'T DROP OFF the page. But if you load the page, drop off, load another page and then come back, the cookie still only get set and doesn't get read out and decreased... But what I want is, that everytime, no matter if the user leaves the page and comes back or if he only reloads the page, the value of the cookie get decreased... Right now that happens only if you load the page multiple times consecutively, not if you drop off and come back...

My question now is, how can I implement that?

You can check this page out by yourself. I am printing out the values of the cookie at the top of the page at the moment!

This is my code so far:

PHP Snippet #1 (set/get Cookie - gets executed in the header)

<?php

    if(is_page(161)){
         if(isset($_COOKIE['limitedProduct'])){
            $old =  $_COOKIE['limitedProduct'];
            $value = $_COOKIE["limitedProduct"] - rand(5,8);
            setcookie('limitedProduct', $value, (time()+31536000));
            echo 'I got set! -> Old: ' . var_export($old) . " - New: " . var_export($value); 
        }else{
            setcookie('limitedProduct', '24', (time()+31536000));
            echo 'I got init! -> 24';
        }   
    }
?>

PHP Snippet #2 (prints out the value of the cookie)

<?php

    //Asking for NULL because at the very first time there is no cookie!

    if($_COOKIE['limitedProduct'] == NULL) {
        echo "<p style='text-align: center; color: red;'><strong>Der Kurs ist stark limitiert - Nur noch 31 verfügbar!</strong></p><br> " . var_export($_COOKIE['limitedProduct']);    
    }else if ($_COOKIE['limitedProduct'] > 2) {
        echo "<p style='text-align: center; color: red;'><strong>Der Kurs ist stark limitiert - Nur noch " . $_COOKIE['limitedProduct'] . " verfügbar!</strong></p>";
    }else if($_COOKIE['limitedProduct'] < 2){
        echo "<p style='text-align: center; color: red;'><strong>Der Kurs ist stark limitiert - Nur noch 2 verfügbar!</strong></p>";
    }

?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?