Magento – Magento set Cookie

cookieproduct

We need to show recently viewed product by customer based on browser and i try to below code.

    $cookie = Mage::getSingleton('core/cookie');

    if ($cookie->get('recent_product')) { 
        $recent_product = $cookie->get('recent_product'); 
        $Current_prd = explode(",",$cookie->get('recent_product'));

        if (!in_array($productId, $Current_prd)) {
            $recent_product=$recent_product.",".$productId;   
        }
        # $cookie->get('recent_product');
        $cookie->set('recent_product', $recent_product ,time()+86400,'/');
    } else { 
        $recent_product =array();
        $recent_product=$productId;
        $cookie = Mage::getSingleton('core/cookie');
        $cookie->set('recent_product',$recent_product ,time()+86400,'/');
    }

It is set the cookie. But when i close the browser, not able to get the previous cookie value.

What i missing the above code?.

Best Answer

Finally I get the solution. Just removed the time() key.

$cookie = Mage::getSingleton('core/cookie');

if ($cookie->get('recent_product')) { 
    $recent_product = $cookie->get('recent_product'); 
    $Current_prd = explode(",",$cookie->get('recent_product'));

    if (!in_array($productId, $Current_prd)) {
        $recent_product=$recent_product.",".$productId;   
    }
    # $cookie->get('recent_product');
    $cookie->set('recent_product', $recent_product ,86400,'/');
} else { 
    $recent_product =array();
    $recent_product=$productId;
    $cookie = Mage::getSingleton('core/cookie');
    $cookie->set('recent_product',$recent_product ,86400,'/');
}