PHP克隆对象会产生多个对象吗?

发布于 2024-11-27 03:47:40 字数 571 浏览 1 评论 0原文

首先,我显示代码:

//item duplication code goes here
elseif($_REQUEST['action'] == 'duplicate'){
    $array_index = $_REQUEST['array_index'];
    $cart = $_SESSION['cart'];
    $tempItem = clone $cart[$array_index];
    $cart[]=$tempItem;
    $_SESSION['cart'] = $cart;
}

例如,如果我的购物车中有 1 件商品,按链接将给我 2 件相同的商品。事实是,直接结果是正确的(我在新选项卡中打开链接)但是在刷新页面(旧选项卡)后,它给了我 3 。

如需解释,请观看一段简短视频:

http://www.youtube.com/watch?v= OORcT5KxZqw

我真的不明白为什么会发生这种情况。任何帮助表示赞赏!

First I show the code:

//item duplication code goes here
elseif($_REQUEST['action'] == 'duplicate'){
    $array_index = $_REQUEST['array_index'];
    $cart = $_SESSION['cart'];
    $tempItem = clone $cart[$array_index];
    $cart[]=$tempItem;
    $_SESSION['cart'] = $cart;
}

For example, if I have 1 item in the shopping cart, pressing the link will give me 2 identical ones. The fact is, the immediate result is correct(I open the link in new tab) But after I refresh the page(old tab), it gives me 3 .

To explain, please see a short video:

http://www.youtube.com/watch?v=OORcT5KxZqw

I really don't understand why it's happening. Any help is appreciated!

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

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

发布评论

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

评论(3

春风十里 2024-12-04 03:47:40

您对代码所做的事情是,如果 action$_REQUEST 等于值 duplicate 那么您将执行以下操作:

  1. 获取购物车从会话(之前存储的)
  2. 克隆购物车并将其存储在 temp 中
  3. ,然后将 temp 复制回购物车
  4. ,最后将购物车存储到会话中
  5. 然后我想您在某处显示您的卡...

这里发生的情况是仅当您单击重复项时,克隆说明才有效 关联。我想您的 PHP 与 html 位于同一个文件中,因此当您在新窗口中打开它时,购物车会更新 2 个项目并存储在会话中(这对于页面的所有实例都是通用的),并且当您重新加载页面时从会话中获取相同的 2 个项目并向您显示。另外,在这里您需要观察 $_REQUEST['action'] 仍然存在,因此它又重复了 1 个项目。现在,每当您刷新页面时,它都会继续复制 1 个项目。

作为此问题的解决方案,请尝试在克隆购物车后使用 unset() 函数取消操作,以便 php 仅在需要时运行。

What you are doing with your code is that if a $_REQUEST of action equals value duplicate then you are doing the following things:

  1. get the cart from the session (which is previously stored)
  2. clone the cart and store it in temp
  3. and then copy back the temp into the cart
  4. and finally store the cart into the session
  5. Then i suppose you display your card somewhere...

What is happening here is that the cloning instructions should be valid only when you click the duplicate link. I suppose you have your PHP in the same file as the html so when you open it in a new window the cart is updated with 2 items and stored in session (which is common to all instances of your page) and when you reload your page the same 2 items are fetched from the session and showed to you. Also here you need to observe that the $_REQUEST['action'] is still present so its duplicating 1 item more. Now whenever you refresh the page it will keep on duplicating 1 item more.

As a solution to this try unsetting the action by using the unset() function after you have cloned the cart so that the php runs only when needed.

海之角 2024-12-04 03:47:40

我怀疑当您刷新页面时它会给您三个,因为旧的 $_REQUEST['array_index'] 仍然存在。您需要设置一个会话变量来指示操作已完成,并在继续执行此操作之前检查它。

elseif($_REQUEST['action'] == 'duplicate'){

    // Check if you already duplicated this array_index
    if (!isset($_SESSION['duplication_done']) || (isset($_SESSION['duplication_done']) &&  $_SESSION['duplication_done'] != $_REQUEST['array_index']) {
      $array_index = $_REQUEST['array_index'];
      $cart = $_SESSION['cart'];
      $tempItem = clone $cart[$array_index];
      $cart[]=$tempItem;
      $_SESSION['cart'] = $cart;

      // After duplicating, store this array_index in $_SESSION
      $_SESSION['duplication_done'] = $_REQUEST['array_index'];
    }
}

I suspect it is giving you three when you refresh the page because the old $_REQUEST['array_index'] is still hanging around. You need to set a session variable to indicate that the action has been completed, and check it before proceeding to do this action.

elseif($_REQUEST['action'] == 'duplicate'){

    // Check if you already duplicated this array_index
    if (!isset($_SESSION['duplication_done']) || (isset($_SESSION['duplication_done']) &&  $_SESSION['duplication_done'] != $_REQUEST['array_index']) {
      $array_index = $_REQUEST['array_index'];
      $cart = $_SESSION['cart'];
      $tempItem = clone $cart[$array_index];
      $cart[]=$tempItem;
      $_SESSION['cart'] = $cart;

      // After duplicating, store this array_index in $_SESSION
      $_SESSION['duplication_done'] = $_REQUEST['array_index'];
    }
}
梦初启 2024-12-04 03:47:40

由于 $_REQUEST 也包含 $_GET,我猜你的 &action=duplicate 位于url 和您的代码会在您重新加载页面时执行。

我建议重定向(使用 header('Location: ...');,请参阅:header) 在复制之后,但仅在从 URL 中删除 action-var 之后。

更新

使用函数getUrlCurrently这里有一个简短的例子:

function getUrlCurrently($filter = array()) {
    $pageURL = isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ? "https://" : "http://";

    $pageURL .= $_SERVER["SERVER_NAME"];

    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= ":".$_SERVER["SERVER_PORT"];
    }

    $pageURL .= $_SERVER["REQUEST_URI"];


    if (strlen($_SERVER["QUERY_STRING"]) > 0) {
        $pageURL = rtrim(substr($pageURL, 0, -strlen($_SERVER["QUERY_STRING"])), '?');
    }

    $query = $_GET;
    foreach ($filter as $key) {
        unset($query[$key]);
    }

    if (sizeof($query) > 0) {
        $pageURL .= '?' . http_build_query($query);
    }

    return $pageURL;
}

if ($_REQUEST['action'] == 'duplicate'){
    // your duplication code here ...

    // after the duplication
    header('Location: ' . getUrlCurrently(array(
        'action', 'array_index' // <--- unsetting the problematic url-vars
    )));
    exit();
}

As $_REQUEST also holds $_GET, I'd guess your &action=duplicate sits in the url and your code get's executed as often as you reload the page.

I would suggest to redirect (using header('Location: ...');, see: header) after the duplication, but only after removing the action-var from the url.

Update:

Using the function getUrlCurrently here's a short example:

function getUrlCurrently($filter = array()) {
    $pageURL = isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ? "https://" : "http://";

    $pageURL .= $_SERVER["SERVER_NAME"];

    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= ":".$_SERVER["SERVER_PORT"];
    }

    $pageURL .= $_SERVER["REQUEST_URI"];


    if (strlen($_SERVER["QUERY_STRING"]) > 0) {
        $pageURL = rtrim(substr($pageURL, 0, -strlen($_SERVER["QUERY_STRING"])), '?');
    }

    $query = $_GET;
    foreach ($filter as $key) {
        unset($query[$key]);
    }

    if (sizeof($query) > 0) {
        $pageURL .= '?' . http_build_query($query);
    }

    return $pageURL;
}

if ($_REQUEST['action'] == 'duplicate'){
    // your duplication code here ...

    // after the duplication
    header('Location: ' . getUrlCurrently(array(
        'action', 'array_index' // <--- unsetting the problematic url-vars
    )));
    exit();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文