1

As I know there isn't a core/session in Magento 2 [source], I've created a custom session using customer/session as given below:

public function __construct(
    \Magento\Customer\Model\Session $customerSession,
    ....
) {
    $this->customerSession = $customerSession;
    ...
}
public function execute() {
    $this->customerSession->setOtp(5); // To set session value
    $this->customerSession->getOtp();  // To get session value
}

$this->customerSession->destroy(); will destroy whole customer session, which I do not want. Is it possible to destroy this particular otp session value automatically after 30 minutes?

amitshree
  • 7,006
  • 12
  • 63
  • 116

1 Answers1

2

You can remove/unset custom session via 'uns' prefix.

$this->customerSession->unsOtp();

Hope this will help you.

Nits
  • 2,496
  • 1
  • 8
  • 21
  • Can you please let me know how to set the time after which it will be automatically destroyed? – amitshree May 23 '18 at 07:27
  • 1
    I think you need to create a custom script and set this script to cron. Cron will run that script every 30min. http://thisinterestsme.com/expire-php-sessions/ this reference link will help you to check the time and unset session. – Nits May 23 '18 at 07:45
  • I think there should be a more simpler solution to automatically unset this value? – amitshree May 24 '18 at 12:13
  • No, I don't think so. Else you can use cookies that will be upset at the particular time. – Nits May 24 '18 at 12:21