设置cookie以使用PHP控制内容

时间:2018-06-27 11:37:18

标签: php cookies

我正在使用Cookie在网页顶部显示隐私警报,由于某种原因,该操作仅在按钮的第二次单击上有效,而不是第一次。

有人能看到为什么会这样吗?

该cookie设置为10秒进行测试。

谢谢

约翰

public Node delete(Object data) {
    Node node = this;

    if (node.data.equals(data)) {
        node = node.next;
        return node;
    }

    while (node.next != null) {
        if (node.next.data.equals(data)) {
            node.next = node.next.next;
        }
        node = node.next;
    }
    return node;
}
public static void main(String[] args) {
    Node node = new Node(10);
    node.add(20);
    node.add(30);

    node = node.delete(10);
    System.out.println("After Deletion of 10");

    node.printList();

    node = node.delete(20);
    System.out.println("After Deletion of 20");

    node.printList();


}

2 个答案:

答案 0 :(得分:1)

实际上,这不是问题,这是Cookie的工作方式。

  

使用在HTTP中发送的Set-Cookie HTTP标头设置Cookie   来自Web服务器的响应。

https://en.wikipedia.org/wiki/HTTP_cookie#Implementation

假设您调用index.php并在其中设置一个cookie,为什么在同一PHP脚本中不可用?由于服务器立即发送标头和正文,因此没有“嘿,先发送此cookie标头,然后再做其他事情”。当PHP脚本结束时,客户端将收到cookie,并发送给它的标头+正文。

要解决您的问题,您可以这样做:

        <?php

       echo "Cookie: ".$_COOKIE['privacy_warning'];

       $privacy_warning = false;

       if (isset($_POST['privacy_button'])) {

       setcookie('privacy_warning', true,  time()+10); // 10 seconds
       $privacy_warning = true;

       }

       if (!privacy_warning):

?>

    <p class="text-center alt">We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices <a href="../privacy/" style="color:white;"><u>here</u></a>. By continuing to use this site you are giving us your consent to do this.<br></p>
    <form action="index.php" method="post" >
       <div class="row">
          <div class="text-center large-12 columns">
             <button class="button tiny success" type="submit" name="privacy_button">ACCEPT</button>
          </div>
       </div>
    </form>

    <?php

       endif;

    ?>

答案 1 :(得分:-1)

您的代码还可以 我在以下代码下运行,效果很好:

<?php

echo "Cookie: ".$_COOKIE['privacy_warning'] . " done \n";
setcookie('privacy_warning', 'ohuuuum',  time()+10);


if (!isset($_COOKIE['privacy_warning'])):

    ?>

    <p class="text-center alt">We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices <a href="../privacy/" style="color:white;"><u>here</u></a>. By continuing to use this site you are giving us your consent to do this.<br></p>
    <form action="index.php" method="post" >
        <div class="row">
            <div class="text-center large-12 columns">
                <button class="button tiny success" type="submit" name="privacy_button">ACCEPT</button>
            </div>
        </div>
    </form>

<?php

endif;

?>

我在运行该代码时将我的cookie设置为“ ohuuuum”,并且当我重新加载页面时,结果是===> Cookie:ohuuuum完成