在PHP中设置多个Cookie [英] Setting Multiple Cookies in PHP

查看:470
本文介绍了在PHP中设置多个Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您的帮助。

我有一个检查登录名然后设置一些cookie的脚本。设置了4个cookie,(稍后我可能会将一些cookie移到服务器会话,但是现在我正在使用cookie。

I have a script that checks logins and then sets some cookies. There are 4 cookies that are being set, (later I will probably move some to server session but for now I am using cookies.

我遇到的问题是在执行脚本的其余部分之前,只设置了前4个中的前2个。有人可以看到原因吗?

The issue that I am having is that only the first 2 of the 4 are being set before the rest of the script is being executed. Can anyone see why?

// 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中,我检查了一下页面右侧的cookie。单击页面,查看页面信息安全性选项卡,查看Cookie 按钮。所有四个Cookie都显示在我的浏览器的该页面上。最后一个没有(因为它没有价值)。

In Firefox, I check to see the cookies for a page by right-clicking on the page, View Page Info, Security tab, View Cookies button. All four cookies show up on that page in my browser. The last one does not (since it is has no value).

我认为您的列名没有正确拼写/大写或其他一些问题,那 $ row ['Access_Level'] $ row ['Db_Con'] 是结果中的空列。

I'm thinking your column names are not spelled/capitalized correctly, or some other issue, and that $row['Access_Level'] and $row['Db_Con'] are empty columns in your result.

这篇关于在PHP中设置多个Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆