cancel
Showing results for 
Search instead for 
Did you mean: 

How to detect browser cookie is expired?

SOLVED

How to detect browser cookie is expired?

I would like to know how to detect browser cookie is expired from Controller.

Any suggestions would be appreciated. Thanks in advance.

5 REPLIES
Highlighted

Re: How to detect browser cookie is expired?

Hi ,
You can able to get using js.

You can not able to get using controller.

If it will help you then Mark as solution.

Re: How to detect browser cookie is expired?

Sorry to ask again.

 

I need to know how to detect cookie is expired on a car page.

 

require(['jquery',  'Magento_Checkout/js/action/get-payment-information',
'Magento_Checkout/js/model/quote',
'mage/storage',   'mage/cookies'], function($,   getPaymentInformationAction, quote, cookies){
$(document).on( "click", ".product-custom-option", function() {

  // I would like to detect if cookie is expired here, so I can empty the cart.

    });

Re: How to detect browser cookie is expired?

Hello,

 

you can do using php code as well

 

<?php

    function secToDays($sec){
        return ($sec / 60 / 60 / 24);
    }

    if(isset($_COOKIE['cookie'])){

        if(round(secToDays((intval($_COOKIE['cookie']) - time())),1) < 1){            echo "Cookie will expire today";
        }else{            echo "Cookie will expire in " . round(secToDays((intval($_COOKIE['cookie']) - time())),1) . " day(s)";
        }
    }else{        echo "Cookie not set...";
    }

?>

 

Re: How to detect browser cookie is expired?

You can find cookie if not expire using cookie get method, below is the example for get cookies, Using $.mage.cookies.get('cookiename') where cookiename is your cookie name.

 require(['jquery',
            'Magento_Checkout/js/action/get-payment-information',
            'Magento_Checkout/js/model/quote',
            'mage/storage',
            'mage/cookies'],
            function($,getPaymentInformationAction, quote, cookies){
                $(document).on( "click", ".product-custom-option", function() {
                    if ($.mage.cookies.get('cookiename') ){ 
                        //if not expired got value of cookie
                    } else { 
                        //cookie expires
                    }
                });
            });
If Issue Solved, Click Kudos/Accept As solutions.
More info visit Blogs

Re: How to detect browser cookie is expired?

Thank you for your replies.

I wonder which name would be the best one to detect cart session.

I am using "form_key" at the moment.