Autor Zpráva
Madenio
Profil
Ahojte,

prosim potreboval by som pomoc so zapisom do cookies. Mam nasledovny kod a jednoducho mi to po uvodnej validacii parametrov nechce zapisat premennu do cookie.
Potrebujem tam nacpat pole. Myslel som, ze je problem v tom, tak som tu premennu uplne zjednodusil a aj tak nic.

<?php
    switch ($subaction){
        case "addproduct":
            if (($productid) && (is_numeric($productid))){
                $query = "SELECT * FROM products WHERE product_id = '".DiscardJunk($productid,'all')."' LIMIT 1";
                $result = GetDBData($query,'fetch_row');
                if ($result){
                    if (($_POST["qty"]) && (is_numeric($_POST["qty"])) && ($_POST["qty"] > 0)){
                        if (!($_COOKIE["cart"][$productid])){
                            //Product not in cart yet, so let's add it now
                            $result = setcookie("cart","1", time()+1209600);
                            header("Location: /cart/productid-".$productid."/addresult-true");
                            die;
                        } else {
                            //Product already in cart, let's just add the quantity
                            $quantity = $cookie["cart"][$productid]["quantity"] + $_POST["qty"];
                            setcookie("cart[".$productid."][quantity]",DiscardJunk($_POST["quantity"],'all'),time()+1209600);
                            header("Location: /cart/productid-".$productid."/addresult-true");
                            die;
                        }
                    } else {
                        //Missing or invalid quantity
                        header("Location: /p/productid-".$productid."/"); 
                        die;
                    }    
                } else {
                    //No such record
                    header("Location: /");
                    die;
                }
            } else {
                //Missing or invalid credentials
                header("Location: /");
                die;
            }
        break;
        default:
            var_dump($_COOKIE);
            $page_content = $smarty->fetch('cart/cart.tpl');
        break;
    }
?>
Madenio
Profil
Problem vyriesil hint na php.net

When setting a cookie on a page that redirects, the cookie must be set after the call to header('Location: ....');

Such as:
<?php
header('Location: http://www.example.com/');
setcookie('asite', $site, time()+60*60, '/', 'site.com');
?>

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0