【问题标题】:Prevent cookie from getting other pages?防止cookie获取其他页面?
【发布时间】:2016-10-11 15:44:07
【问题描述】:

我不想允许从其他页面获取 cookie !我已经在互联网上搜索过,但没有真正找到,或者我可能不知道如何提及那个案例。我该如何处理!我想在test2.php 中获取空值,但在test.php 中获取cookie?

test.php

<?php
setcookie("acc_id", "23A", time() + 3600, '/');
header("test.php");
var_dump($_COOKIE); // 'acc_id' => string '23A' (length=3)
?>

test2.php

<?php
var_dump($_COOKIE); // 'acc_id' => string '23A' (length=3)

【问题讨论】:

标签: php security cookies


【解决方案1】:

您可以在 setcookie 语法中使用$path 参数。

setcookie("acc_id", "23A", time() + 3600, '/test.php');

现在,如果您在 test2.php 中尝试 print_r($_COOKIE['acc_id']);,它会显示 未定义索引,这意味着该页面没有设置 cookie。

【讨论】:

    猜你喜欢
    • 2015-02-26
    • 1970-01-01
    • 2013-05-29
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    相关资源
    最近更新 更多