无法正确显示 cookie 计数
我有一个文件 basket.php
可以完美地显示计数,但在另一个 php 文件 product.php
中它始终显示 0,编码如下:
basket.php
<?php
//include(dirname(__FILE__)."/../config.php");
if (isset($_COOKIE["products"])) {
//Count of all products in basket
$BasketCount = count($_COOKIE['products']);
//Loop through and get each cookie
foreach ($_COOKIE['products'] as $name) {
$name = htmlspecialchars($name);
echo "$name <a href='remove.php?remove=$name'>Click here to remove from basket</a> <br />\n";
}
echo "Basket Count: $BasketCount";
}else{
echo "Basket is empty";
}
?>
产品。 php (只是获取篮子计数的行)
$basketcount = count($_COOKIE['products']);
这是我设置cookie的方法
addtobasket.php
<?php
include(dirname(__FILE__)."/../config.php");
$product = $_GET['p'];
setcookie("products[$product]", $product);
echo "$product added to basket";
//Show current basket products
?>
I have one file basket.php
that displays the count perfectly, but in the other php file product.php
it always displays 0 the codings are below:
basket.php
<?php
//include(dirname(__FILE__)."/../config.php");
if (isset($_COOKIE["products"])) {
//Count of all products in basket
$BasketCount = count($_COOKIE['products']);
//Loop through and get each cookie
foreach ($_COOKIE['products'] as $name) {
$name = htmlspecialchars($name);
echo "$name <a href='remove.php?remove=$name'>Click here to remove from basket</a> <br />\n";
}
echo "Basket Count: $BasketCount";
}else{
echo "Basket is empty";
}
?>
product.php
(just the line that gets the basket count)
$basketcount = count($_COOKIE['products']);
Here is how i set the cookies
addtobasket.php
<?php
include(dirname(__FILE__)."/../config.php");
$product = $_GET['p'];
setcookie("products[$product]", $product);
echo "$product added to basket";
//Show current basket products
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
路径可能有问题 - http://www.php.net/manual/en /function.setcookie.php,
$path
参数。或域。May be issue with path - http://www.php.net/manual/en/function.setcookie.php,
$path
parameter. Or domain.不是实际的答案,但仍然相关:
假设您正在制作一个带有购物篮/购物车系统的商业网站,我建议这样做:
不要依赖 COOKIE,永远不要。它存储在客户端并且可以轻松修改。此外,有些浏览器会直接拒绝它们,因此您的购物篮将无法使用。
使用
$_SESSION[]
代替,它们只存储客户端的标识符。更安全,可以防止黑客代码缺陷和任何其他情况。Not the actual answer, but nevertheless related:
Assuming you're making a commercial website with a basket/shopping cart system, I would advice this:
DO NOT RELY ON COOKIES, never. It's stored on client side and can be modified easily. Moreover some browser simply refuses them, and thus your basket won't work.
Use
$_SESSION[]
instead, they only store the identifier client side. Safer, both against hacking code flaws and anything.可能你在设置cookie之前运行product.php..
除了该代码对我来说没问题。
或者您可以通过此代码检查 cookie 是否设置..
if(isset($_COOKIE['products'][$product])){
}
这样您就会有确切的想法..
may be you are running product.php before cookie is set..
other than that code is fine to me.
or you can check cookie is set or not by this code..
if(isset($_COOKIE['products'][$product])){
}
so that you will have exact idea..