Why is php setcookie() function not working?

34
February 09, 2019, at 12:40 PM

my problem is that cookie is not working when i call php function using ajax request. i'm not getting any error or warning when click on add to cart button but still cookie is not set. Can anyone help me to solved this problem ?

i do following JS code:

function addtocart(item_id,vend_id,mfg_part,cond,qty){
        $('.loadingOverlayDiv').show();
        $.ajax({
            type: "POST",
            url: "ajax_request.php",
            data: {"item_id":item_id,"vend_id":vend_id,"mfg_part":mfg_part,"cond":cond,"qty":qty},
            success:function(result) {
                alertify.success("Item added to cart.");
            }
        });
    }

and here is php code in which i'm calling function of php class.

// Add to cart
if(isset($_POST['item_id']) && isset($_POST['vend_id']) && isset($_POST['mfg_part']) && isset($_POST['cond']) && isset($_POST['qty'])) {
    $orderObj->addToCart($_POST['item_id'],$_POST['vend_id'],$_POST['mfg_part'],$_POST['cond'],$_POST['qty']);
}

this one is class function addToCart:

public function addToCart($id,$vend_id,$mfg_part,$cond,$qty) {
        $qty=$qty<=0 ? 1 : $qty;
        $key = $id.'-'.$vend_id.'-'.$cond;
        // add new item on array
        $cart_items[$key]=array(
            'item_id'=>$id,
            'vend_id'=>$vend_id,
            'condition'=>$cond,
            'quantity'=>$qty
        );
        $cookie = isset($_COOKIE['cart_list']) ? $_COOKIE['cart_list'] : "";
        $cookie = stripslashes($cookie);
        $saved_cart_items = json_decode($cookie, true);
        //If $saved_cart_items is null, prevent null error
        if(!$saved_cart_items){
            $saved_cart_items=array();
        }
        if(count($saved_cart_items)>0){
            foreach($saved_cart_items as $key=>$value){
                // add old item to array, it will prevent duplicate keys
                $cart_items[$key]=array(
                    'item_id'=>$id,
                    'vend_id'=>$vend_id,
                    'condition'=>$cond,
                    'quantity'=>$value['quantity']
                );
            }
        }
        //Put item to cookie
        $json = json_encode($cart_items, true);
        setcookie("cart_list", $json, floatval(time()) + floatval(86400 * 30), '/'); // 86400 = 1 day
        $_COOKIE['cart_list']=$json;
        return 1;
    }

Working well on localhost but not working on Server

READ ALSO
React binding function

React binding function

I have seen a lot of reactjs places where a function is called like below

46
how do I push, pop, and remove certain values from mysql json array in sequelize

how do I push, pop, and remove certain values from mysql json array in sequelize

I have a column with a sequelize type of sequelizeJSON I added an array to it and it shows up in mysql via workbench like this: []

52
Where does the callback within this express app coming from?

Where does the callback within this express app coming from?

after some freecodecamp I started doing the Express js tutorial from MDN (https://developermozilla

42
HTML question in how to change a file using tables to now use layers [on hold]

HTML question in how to change a file using tables to now use layers [on hold]

I'm new to programming and got landed with a not so good professor for my first programming course so I'm trying to teach myself a lot of things

26