I would like to know how to detect browser cookie is expired from Controller.
Any suggestions would be appreciated. Thanks in advance.
Solved! Go to Solution.
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..."; } ?>
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. });
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..."; } ?>
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 } }); });
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.