PHP克隆对象会导致多个对象?
首先,我显示代码:PHP克隆对象会导致多个对象?,php,count,clone,Php,Count,Clone,首先,我显示代码: //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'
//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
如需解释,请观看短片:
我真的不明白为什么会这样。感谢您的帮助 我怀疑当你刷新页面时,它会给你三个,因为旧的
$\u请求['array\u 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'];
}
}
我怀疑当你刷新页面时,它会给你三个提示,因为旧的
$\u请求['array\u 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'];
}
}
同样,我猜您的&action=replicate
位于url中,并且您的代码会在重新加载页面时执行
我建议在复制后重定向(使用标题('Location:…');
,请参阅:),但仅在从url中删除操作
-var之后
更新:
使用此函数,下面是一个简短的示例:
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();
}
函数GetUrlCurrent($filter=array()){
$pageURL=isset($_服务器[“HTTPS”])&&&$_服务器[“HTTPS”]=“on”?“HTTPS://”:“http://”;
$pageURL.=$\u服务器[“服务器名称”];
如果($\服务器[“服务器\端口”!=“80”){
$pageURL.=“:”$\u服务器[“服务器\u端口”];
}
$pageURL.=$\u服务器[“请求URI”];
如果(strlen($\u服务器[“查询字符串])>0){
$pageURL=rtrim(substr($pageURL,0,-strlen($\u SERVER[“QUERY\u STRING”])),“?”);
}
$query=$\u GET;
foreach($filter as$key){
未设置($query[$key]);
}
如果(sizeof($query)>0){
$pageURL.='?'.http_build_query($query);
}
返回$pageURL;
}
如果($_请求['action']=='duplicate'){
//您的复制代码在这里。。。
//复制之后
标头('位置:')。GetUrlCurrent(数组(
‘action’、‘array_index’//同样适用,我猜您的&action=duplicate
位于url中,您的代码会在重新加载页面时执行
我建议在复制后重定向(使用标题('Location:…');
,请参阅:),但仅在从url中删除操作
-var之后
更新:
使用此函数,下面是一个简短的示例:
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();
}
函数GetUrlCurrent($filter=array()){
$pageURL=isset($_服务器[“HTTPS”])&&&$_服务器[“HTTPS”]=“on”?“HTTPS://”:“http://”;
$pageURL.=$\u服务器[“服务器名称”];
如果($\服务器[“服务器\端口”!=“80”){
$pageURL.=“:”$\u服务器[“服务器\u端口”];
}
$pageURL.=$\u服务器[“请求URI”];
如果(strlen($\u服务器[“查询字符串])>0){
$pageURL=rtrim(substr($pageURL,0,-strlen($\u SERVER[“QUERY\u STRING”])),“?”);
}
$query=$\u GET;
foreach($filter as$key){
未设置($query[$key]);
}
如果(sizeof($query)>0){
$pageURL.='?'.http_build_query($query);
}
返回$pageURL;
}
如果($_请求['action']=='duplicate'){
//您的复制代码在这里。。。
//复制之后
标头('位置:')。GetUrlCurrent(数组(
“action”、“array_index”//您对代码所做的是,如果
action
的$\u请求等于值duplicate
,则您正在执行以下操作:
从会话中获取购物车(先前已存储)
克隆购物车并将其存储在temp中
然后将临时文件复制回购物车
最后将购物车存储到会话中
那么我想你应该在某处展示你的名片
这里发生的事情是,只有当您单击复制链接时,克隆说明才有效。我假设您的PHP与html位于同一个文件中,因此当您在新窗口中打开它时,购物车将更新为2个项目并存储在会话中(这对于您页面的所有实例都是通用的)当您重新加载页面时,将从会话中提取相同的2项并显示给您。同时,您需要注意,$\u请求['action']
仍然存在,因此它将多复制1项。现在,每当您刷新页面时,它将继续多复制1项
作为一种解决方案,在克隆购物车后,尝试使用unset()
函数取消操作,以便php仅在需要时运行。使用代码所做的是,如果action
的$\u请求等于值replicate
,则执行以下操作:
从会话中获取购物车(先前已存储)
克隆购物车并将其存储在temp中
然后将临时文件复制回购物车
最后将购物车存储到会话中
那么我想你应该在某处展示你的名片
这里发生的事情是,只有当您单击复制链接时,克隆说明才有效。我假设您的PHP与html位于同一个文件中,因此当您在新窗口中打开它时,购物车将更新为2个项目并存储在会话中(这对于您页面的所有实例都是通用的)当您重新加载页面时,将从会话中提取相同的2项并显示给您。同时,您需要注意,$\u请求['action']
仍然存在,因此它将多复制1项。现在,每当您刷新页面时,它将继续多复制1项
作为一种解决方案,在克隆购物车后,尝试使用unset()
函数取消操作,以便php仅在需要时运行。是吗