将变量数组添加到 Cookie 然后出现错误
输入一些变量后,
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如错误所示,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.
我们可以在 cokkie 中一次添加一个变量
所以这样做
We can add one variable at a time in cokkie
so do this