dongyi9330 2018-08-11 18:52 采纳率: 100%
浏览 15

Yii2 - 压缩cookie值或更好的替代品

I am dealing with a existing code which stores the order item details in a cookie.

in Controller:

public function actionAdd2cart($id, $url, $instructor_id, $location_id, $start_time, $date1) {
    $ep = Yii::$app->getTable->settings('general','enable_payment');
   $order_item = \Yii::$app->getRequest()->getCookies()->getValue('order_item');
        {
    $model = ClassDuration::find()->where(['id' => $id])->One();
    $model1 = Instructor::find()->where(['user_id' => $instructor_id])->One();
    $time = strtotime($start_time);
    $duration = $model1['instructor_duration'];
    $endTime = date("h:i A", strtotime('+' . $duration . ' minutes', $time));
    $fees = Instructor::find()->where(['user_id' => $model['instructor_id']])->one();                 
    $order_item[] = [
        'duration' => date('h:i A', strtotime($start_time)) . '-' . $endTime,                                
        'starttime' => $start_time,
        'endtime' => $endTime,
        'id' => $model->id,
        //'price' => $fees['instructor_fees'], this is disabled as payment info is not needed and to increase the cookie size
        'q' => 1,
        'date' => $model->start_date,
        'instructor_id' => $instructor_id,
        'location_id' => $location_id,
        'date1' => $date1,
        'datetime' => $date1 . "-" . $start_time . "i" . $instructor_id,
        ]; //product details array
                }

/********** cookies for product details **********/

        $cookie = new \yii\web\Cookie([
            'name' => 'order_item',
            'value' => $order_item,
            'expire' => time() + (60 * 60 * 24),
        ]);
    Yii::$app->getResponse()->getCookies()->add($cookie); // add product deatils to cookies
        if ($url == 'view') {
            return $this->redirect(Yii::$app->request->referrer);
        } else {
            // echo 1;
        }
    }

Now the problem I am facing with this code is like while adding the item to cart, it allow for 6-8 items being added in the cart depending on diffrent browser and due to cookie size limitation, I am not able to add more items to cart beyond that.

I am looking for a way like if the cookie value can be compressed before setting the cookies and uncompressed while accessing it.

or some other suitable solution.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私