将变量数组添加到 Cookie 然后出现错误

发布于 2025-01-06 14:22:20 字数 482 浏览 2 评论 0原文

输入一些变量后,

PHP代码:

$CartItem = array(
        'itemID' => $itemID,
        'ProductName' => $ProductName,
        'Option' => $Option,
        'Quantity' => $Quantity,
        'Price' => $Price,
        'Total' => $Total
    );

setcookie('CartItem', $CartItem, null);

输出错误:

Warning: setcookie() expects parameter 2 to be string, array given in /Users/user/Sites/app/addtocart.php on line 46

After input some variable,

PHP code:

$CartItem = array(
        'itemID' => $itemID,
        'ProductName' => $ProductName,
        'Option' => $Option,
        'Quantity' => $Quantity,
        'Price' => $Price,
        'Total' => $Total
    );

setcookie('CartItem', $CartItem, null);

Output error:

Warning: setcookie() expects parameter 2 to be string, array given in /Users/user/Sites/app/addtocart.php on line 46

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一枫情书 2025-01-13 14:22:20

正如错误所示,cookie 数据必须是字符串格式。更重要的是,您不应该将真实数据存储在 cookie 中,因为用户可以编辑它们。

由于您使用的是 PHP,因此您可以利用会话...您可以使用 session_start() 它将设置一个您不需要管理的 cookie,然后您可以在 < code>$_SESSION 变量服务器端。

请参阅此处: http://php.net/manual/en/function.session- start.php

请注意,在每个页面上输出任何内容之前,必须调用 session_start()。

As the error suggests, cookie data must be in string format. More importantly, you shouldn't store real data in a cookie, because the user can edit them.

Since you're using PHP, you can utilize sessions... You can use session_start() which will set a cookie that you don't need to manage, and then you can set data in the $_SESSION variable server-side.

See here: http://php.net/manual/en/function.session-start.php

Note that session_start() must be called before anything is output on each page.

心舞飞扬 2025-01-13 14:22:20

我们可以在 cokkie 中一次添加一个变量
所以这样做

$CartItem[] = array(
        'itemID' => $itemID,
        'ProductName' => $ProductName,
        'Option' => $Option,
        'Quantity' => $Quantity,
        'Price' => $Price,
        'Total' => $Total
    );

foreach($CartItem as $key=>$value)
{
setcookie($key, $value, null);

}

We can add one variable at a time in cokkie
so do this

$CartItem[] = array(
        'itemID' => $itemID,
        'ProductName' => $ProductName,
        'Option' => $Option,
        'Quantity' => $Quantity,
        'Price' => $Price,
        'Total' => $Total
    );

foreach($CartItem as $key=>$value)
{
setcookie($key, $value, null);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文