Keep session after closing browser using PHP

0

I'm making a cart using session , can you increase the time of session ?

Below is the class I created for my shopping cart.

class carrinhocompra{
public function __construct(){
    if(!isset($_SESSION['carrinho'])){
        $_SESSION['carrinho'] = array();
    }
}

public function adicionar($id, $qtd=1, $form_id=NULL){
    if(is_null($form_id)){
        $indice = sprintf("%s:%s", (int)$id, 0);

    } else {
        $indice = sprintf("%s:%s", (int)$id, (int)$form_id);
    }

    if(!isset($_SESSION['carrinho'][$indice])){
        $_SESSION['carrinho'][$indice] = (int)$qtd;
    }


}

public function aterarQtd($indice, $qtd){
    if(isset($_SESSION['carrinho'][$indice])){
        if($qtd > 0){
            $_SESSION['carrinho'][$indice] = (int)$qtd;
        }
    }
}

public function excluirProd($indice){
   unset($_SESSION['carrinho'][$indice]);
}

}

    
asked by anonymous 28.07.2015 / 02:43

1 answer

2

For this you need to use cookies :

setcookie('carrinho', $_SESSION['carrinho'], time() + 60 * 60 * 24);
//Esse cookie expira em um dia, você pode alterar o valor de acordo com a sua necessidade

And when it comes to showing the cart, you check that cookie exists:

$carrinho = $_COOKIE['carrinho'];
    
28.07.2015 / 03:03