//以商品为单位分别存入到各个不同的cookie中,避免因为cookie值过大,导致数据存储失败 $cart_info_one[brand_name] = $parent[‘brand_name‘]; setcookie("cart_info_".$maxindex,serialize($cart_info_one),time()+315360000,‘/‘); //获取cookie中购物车中信息 $cart_info_arr[] = unserialize(stripslashes($_COOKIE["$cookie_name"]));//cookie 购物车信息 //主要函数封装方法 /* * 用户登录后将本地购物车cookie和数据库信息进行同步 * */ function syn_cart_cookie(){ if(empty($_SESSION[‘user_id‘])){ return; } $sql = "SELECT goods_id FROM ".$GLOBALS[‘ecs‘]->table(‘cart‘)." WHERE user_id=".$_SESSION[‘user_id‘]; $cart_goods_id = $GLOBALS[‘db‘]->getAll($sql); foreach ($cart_goods_id as $cart_goods_id_list){ $db_cart_goods_id[] = $cart_goods_id_list[‘goods_id‘]; } $cart_info_arr = cookie_cart_info();//cookie 购物车信息 if(!is_array($cart_info_arr)){ return ; } foreach ($cart_info_arr as $key=>$cart_info_list){ $cart_info = array(); if(!in_array($cart_info_list[‘goods_id‘], $db_cart_goods_id)){ unset($cart_info_list[‘rec_id‘]); $cart_info_list[‘user_id‘] = $_SESSION[‘user_id‘]; $cart_info_list[‘session_id‘] = $_SESSION[‘session_id‘]; $GLOBALS[‘db‘]->autoExecute($GLOBALS[‘ecs‘]->table(‘cart‘), $cart_info_list, ‘INSERT‘); } } clean_cookie_cart();//清除购物车cookie信息 } /* 获取cookie中购物车的名称 */ function cookie_cart_index(){ $cookie_keys = array_keys($_COOKIE); foreach ($cookie_keys as $cookie_keys_list){ if(strpos($cookie_keys_list,‘cart_info_‘) !== false && is_numeric(ltrim($cookie_keys_list,‘cart_info_‘))){ $index[] = ltrim($cookie_keys_list,‘cart_info_‘); } } if(empty($index)){ return false; } return $index; } /* 获取cookie中购物车最大的索引值 */ function cookie_cart_maxIndex(){ $index = cookie_cart_index(); if($index === false){ return -1; } rsort($index); return $index[0]; } /* 将cookie中的购物车信息合拼为一个二维数组 */ function cookie_cart_info(){ $index = cookie_cart_index(); if($index === false){ return; } $cart_info_arr = array(); foreach ($index as $index_info){ $cookie_name = ‘cart_info_‘.$index_info; $cart_info_arr[] = unserialize(stripslashes($_COOKIE["$cookie_name"]));//cookie 购物车信息 } return $cart_info_arr; } /* 清除cookie中购物车信息 */ function clean_cookie_cart(){ $index = cookie_cart_index(); foreach ($index as $cart_info_dex){ setcookie(‘cart_info_‘.$cart_info_dex,‘‘,time()-100,‘/‘); } return true; }
时间: 2024-10-12 12:56:14