无法正确显示 cookie 计数

发布于 2024-11-25 12:11:18 字数 1025 浏览 0 评论 0原文

我有一个文件 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 技术交流群。

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

发布评论

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

评论(3

心房敞 2024-12-02 12:11:18

路径可能有问题 - 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.

一梦等七年七年为一梦 2024-12-02 12:11:18

不是实际的答案,但仍然相关:

假设您正在制作一个带有购物篮/购物车系统的商业网站,我建议这样做:
不要依赖 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.

记忆里有你的影子 2024-12-02 12:11:18

可能你在设置cookie之前运行product.php..

除了该代码对我来说没问题。

或者您可以通过此代码检查 cookie 是否设置..

if(isset($_COOKIE['products'][$product])){

  echo "cookie is set..";

}

这样您就会有确切的想法..

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])){

  echo "cookie is set..";

}

so that you will have exact idea..

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