防止cookie获取其他页面?

时间:2016-10-11 07:32:27

标签: php security cookies

我不想允许从其他页面获取cookie!我在互联网上搜索过但没找到,或者可能是我不知道如何提及那个案例。我怎么能管理它!我想在test2.php中获取null,但在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)

1 个答案:

答案 0 :(得分:2)

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

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

现在,如果你从test2.php尝试print_r($_COOKIE['acc_id']);,它会显示 Undefined Index ,这意味着没有为该页面设置cookie。

I have named the page as text.php in my environment.

相关问题