Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

arcadia_1's avatar

Session Problem

With php artisan route:list i've found this line, it's used to insert a room of an hotel in the cart, the ajax call save it in the Session with this post route


| POST| api/set/cart/room   | App\Http\Controllers\HotelController@api_set_cart_room  | web,api,cors 

I don't know why, but from other pages of my applications Session::all() return in the cart_session an empty array, i've already double checked all my names and codes, so i don't know where i can look.

Edit:

This is the code that upload the Session

 Session::put('cart_session',$temp);
            if(Session::has('cart_session') && count(Session::get('cart_session'))<=0){
                session()->forget('cart_session');
                session(['cart_session'=>$temp]);
                session()->save;
            }elseif(!Session::has('cart_session')){
                session(['cart_session'=>$temp]);
            }
0 likes
3 replies
jlrdw's avatar

| POST| api ...

Is session started.

Snapey's avatar

I haven't a clue what your code is supposed to do?

You put something in session and then on the next line, check if session has it, first by checking with has and then checking that the number of cart session is less than or equal to 0 (how can these both ever be true?)

But if is not true, you check again if session has cart_session and if not, add it?

WTF?

This is code that updates session

Session::put('cart_session',$temp);

What's the rest for?

arcadia_1's avatar
arcadia_1
OP
Best Answer
Level 1

Here the total function, is a add room to cart function.

public function api_set_cart_room(Request $request){
        if(isset($request->room_id)){
            $cart_items_room = [];
            $session = Session::all();
            if(Session::has('is_paypal') && Session::get('is_paypal')==true){
                array_push($cart_items_room,[
                    'temp_room_id'=>sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
                        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
                        mt_rand( 0, 0xffff ),
                        mt_rand( 0, 0x0fff ) | 0x4000,
                        mt_rand( 0, 0x3fff ) | 0x8000,
                        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
                    ),
                    'room_id'=> isset($request->room_id)?$request->room_id:null,
                    'startdate'=>isset($session['searchstartdate'])?intval($session['searchstartdate']):null,
                    'enddate'=>isset($session['searchenddate'])?intval($session['searchenddate']):null,
                    'adults'=> isset($session['searchadult'])?intval($session['searchadult']):1,
                    'children'=> isset($session['searchchild'])?intval($session['searchchild']):0,
                    'children_age'=> isset($session['searchchildage'])?$session['searchchildage']:[],
                    'price_full'=> isset($request->price_full)?$request->price_full:0,
                    'price_discounted'=> isset($request->price_discounted)?$request->price_discounted:0,
                    'discount_array'=>isset($request->discount)?$request->discount:[],
                    'meals'=>isset($session['optradio'])?intval($session['optradio']):1,
                    'is_paypal'=>true
                ]);
            }else{
                array_push($cart_items_room,[
                    'temp_room_id'=>sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
                        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
                        mt_rand( 0, 0xffff ),
                        mt_rand( 0, 0x0fff ) | 0x4000,
                        mt_rand( 0, 0x3fff ) | 0x8000,
                        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
                    ),
                    'room_id'=> isset($request->room_id)?$request->room_id:null,
                    'startdate'=>isset($session['searchstartdate'])?intval($session['searchstartdate']):null,
                    'enddate'=>isset($session['searchenddate'])?intval($session['searchenddate']):null,
                    'adults'=> isset($session['searchadult'])?intval($session['searchadult']):1,
                    'children'=> isset($session['searchchild'])?intval($session['searchchild']):0,
                    'children_age'=> isset($session['searchchildage'])?$session['searchchildage']:[],
                    'price_full'=> isset($request->price_full)?$request->price_full:0,
                    'price_discounted'=> isset($request->price_discounted)?$request->price_discounted:0,
                    'discount_array'=>isset($request->discount)?$request->discount:[],
                    'meals'=>isset($session['optradio'])?intval($session['optradio']):1,
                    'is_paypal'=>false
                ]);
            }
            $room = DB::table('room_details')->where('id', $request->room_id)->first();
            $data = [
                'cart_item_room'=>$cart_items_room[0],
                'room'=>$room,
            ];
            if(isset($request->room_qta) && $request->room_qta>1){
                $temp = [];
                for($i = 0; $i<$request->room_qta;$i++) {
                    array_push($temp, $cart_items_room[0]);
                    $temp[$i]['temp_room_id']=sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
                        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
                        mt_rand( 0, 0xffff ),
                        mt_rand( 0, 0x0fff ) | 0x4000,
                        mt_rand( 0, 0x3fff ) | 0x8000,
                        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
                    );
                }
                $cart_items_room = null;
                $cart_items_room = $temp;
            }
            $cart_session = isset($session['cart_session'])?$session['cart_session']:[];
            $temp = array();
            if($cart_session==[]){
                session(['cart_session'=>$cart_items_room]);
            }else {
                foreach ($cart_session as $key => $item) {
                    array_push($temp, $item);
                }
                foreach ($cart_items_room as $key => $item) {
                    array_push($temp, $item);
                }
                session(['cart_session'=>$temp]);
            }
            return view('html_parts.cart_item_html',$data);
        }else{
            return dd($request->all());
        }
    }

But i've found the solution.

When you send an ajax request to change with Session::put/get or similar, if the ajax is async or looped in some cicle the Session does not work due to read/write file of the session.

The solution is to make the request sync or to insert a setTimeout of 500ms

here the stack overflow: https://stackoverflow.com/questions/24031562

Ps. the code is the first cast version without a correction or an optimization, so is bad.

Please or to participate in this conversation.