感谢您的帮助。
我有一个检查登录然后设置一些 cookie 的脚本。正在设置 4 个 cookie,(稍后我可能会将一些 cookie 移动到服务器 session ,但现在我正在使用 cookie。
我遇到的问题是,在执行脚本的其余部分之前,只设置了 4 个中的前 2 个。谁能看出原因?
// If there is a row, and only 1 row, then the details are correct. Set the cookie and re-direct
$row = mysqli_fetch_array($data);
setcookie('user_id', $row['User_ID'], time()+ (60*15),"/");
setcookie('user_name', $row['UserName'], time()+ (60*15),"/");
setcookie('access_Level', $row['Access_Level'], time()+ (60*15),"/");
setcookie('db_con', $row['Db_Con'], time()+ (60*15),"/");
$home_url = $link . "application/views/Dashboard.php";
header('Location: ' . $home_url);
最佳答案
使用:
<?php
$row = array(
'User_ID' => '1',
'UserName' => '1',
'Access_Level' => '1',
'Db_Con' => '1'
);
setcookie('user_id', $row['User_ID'], time()+ (60*15),"/");
setcookie('user_name', $row['UserName'], time()+ (60*15),"/");
setcookie('access_Level', $row['Access_Level'], time()+ (60*15),"/");
setcookie('db_con', $row['Db_Con'], time()+ (60*15),"/");
// This will not be set, and the cookie is not created.
setcookie('db_con2', $row['Db_Con2'], time()+ (60*15),"/");
?>
http://jfcoder.com/test/setcookie.php
在 Firefox 中,我通过右键单击页面、查看页面信息
、安全
选项卡、查看 Cookies< 来查看页面的 cookie/
按钮。所有四个 cookie 都显示在我浏览器的那个页面上。最后一个没有(因为它没有值(value))。
我认为您的列名称拼写/大写不正确,或者其他一些问题,$row['Access_Level']
和 $row['Db_Con']
是结果中的空列。
关于php - 在 PHP 中设置多个 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6726522/