购物车信息存cookie
Posted CIBLE
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了购物车信息存cookie相关的知识,希望对你有一定的参考价值。
//以商品为单位分别存入到各个不同的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; }
以上是关于购物车信息存cookie的主要内容,如果未能解决你的问题,请参考以下文章
关于Android的okHttp+webview的cookie跨域问题
作业二:优化购物车:用户入口:1.将商品的信息存到文件中;2.将已经购买的商品余额记录存到文件中。商家入口:1.可以添加商品;2.可以修改商品的价格