Page 1 of 2

How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Sat Sep 21, 2019 3:32 am
by rliston
I have OpenCart 3.0.3.2 using OCMOD. I'd like the logged in customer session to end after 30 minutes of inactivity and redirect to the account login page, index.php?route=account/login. How do I do this?

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Sat Sep 21, 2019 2:33 pm
by opencartmart
File name: catalog/controller/common/header.php
Find the following line:

Code: Select all

if ($this->customer->isLogged()) {
Now add following lines of code after that.

Code: Select all

if (isset($this->session->data['last']) && (time() - $this->session->data['last'] > 30 * 60)) {
           $this->customer->logout();
            unset($this->session->data['last']);
           $this->response->redirect($this->url->link('account/login', '', true));
 }
$this->session->data['last'] = time();
P.S:
1. It will logout the users at their next activity attempt after 30 minutes
2. It is recommended to use ocmod instead of modifying file directly
3. There is no ajax involvement here therefore it will not redirect automatically

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Sun Sep 22, 2019 1:13 pm
by rliston
That worked, thanks! :)

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Mon Sep 23, 2019 3:05 pm
by opencartmart
Glad to hear. Thanks

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Tue Sep 01, 2020 3:09 pm
by ajadco
Hi, I am new to OpenCart, I tried the code you mentioned earlier for OC 3.0.3.2 but didn't work, can anybody help ???
I want customers who already logged in to their account to be logged out after 30 minutes of inactivity, if the site is still open or closed.
Thanks

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Tue Sep 01, 2020 3:57 pm
by letxobnav
I would use:

Code: Select all

$this->response->redirect($this->url->link('account/login', 'inactive', true));
so you can check the get variable "inactive" on the sign-in page and set a message that the user was signed-out because of inactivity.
otherwise set a message about the 30min inactivity limit on the sign-in page like:

Code: Select all

if (isset($this->request->get['inactive'])) {
  sign out message
} else {
  limit message
}

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Tue Sep 01, 2020 4:44 pm
by ajadco
letxobnav wrote:
Tue Sep 01, 2020 3:57 pm
I would use:

Code: Select all

$this->response->redirect($this->url->link('account/login', 'inactive', true));
so you can check the get variable "inactive" on the sign-in page and set a message that the user was signed-out because of inactivity.
otherwise set a message about the 30min inactivity limit on the sign-in page like:

Code: Select all

if (isset($this->request->get['inactive'])) {
  sign out message
} else {
  limit message
}
I am not sure exactly where to put the code you advised.

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Tue Sep 01, 2020 5:25 pm
by letxobnav
in the file catalog/controller/common/header.php
you have:

Code: Select all

		// Wishlist
		if ($this->customer->isLogged()) {
you can add after that:

Code: Select all

			// automatic sign out after 20 min inactivity
			if (isset($this->session->data['last']) && (time() - $this->session->data['last'] > 20 * 60)) {
				$this->customer->logout();
				unset($this->session->data['last']);
				$this->response->redirect($this->url->link('account/login', 'inactive', true));
			}
			$this->session->data['last'] = time();
in the file catalog/controller/account/login.php
just before:

Code: Select all

		$this->response->setOutput($this->load->view('account/login', $data));
you can add:

Code: Select all

		if (isset($this->request->get['inactive'])) {
			$data['message'] = '<p><i>For your security, you have been Signed Out due to 20 min of inactivity</i></p>';
		} else {
			$data['message'] = '<p><i>For your security, you will be Signed Out after 20 min of inactivity</i></p>';
		}
you can later refer those messages to your language files.

in file catalog/view/theme/default/template/account/login.twig
you add wherever you want the message to show:

Code: Select all

{{ message }}

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Wed Sep 02, 2020 3:53 pm
by ajadco
My friend,
I tried the code exactly as you said but no luck, I forgot to tell you that I use Journal 3 on OpenCart 3.0.3.2. I added the last code in:
catalog/view/theme/journal/template/account/login.twig
What do you think the problem is ???

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Wed Sep 02, 2020 5:46 pm
by letxobnav
you cleared the theme cache and any other cache journal might have?

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Thu Sep 03, 2020 1:42 pm
by ajadco
Yes I did but also no luck

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Thu Sep 03, 2020 3:36 pm
by letxobnav
the automatic logout works right?
just the message does not work?

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Thu Sep 03, 2020 4:02 pm
by ajadco
No the automatic logout doesn't work
The most important thing for me is to logout customers automatically after 30 min or 60 min of inactivity in the website
again I use OpenCart 3.0.3.2 with Journal 3 Theme, I also use an extention called: Page after login - strict login (cartbinder@gmail.com)

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Thu Sep 03, 2020 4:34 pm
by letxobnav
then you probably already have a modification running on the header controller and on your login controller.
check in your modification directory if there is a catalog/controller/common/header.php and/or catalog/controller/account/login.php in there.

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Fri Nov 13, 2020 10:43 am
by nightwing
Hey letxobnav, thanks, this worked for my 3.0.3.2 install. How can we force a refresh at this point to redirect to logout page automatically?
Thanks
letxobnav wrote:
Tue Sep 01, 2020 5:25 pm
in the file catalog/controller/common/header.php
you have:

Code: Select all

		// Wishlist
		if ($this->customer->isLogged()) {
you can add after that:

Code: Select all

			// automatic sign out after 20 min inactivity
			if (isset($this->session->data['last']) && (time() - $this->session->data['last'] > 20 * 60)) {
				$this->customer->logout();
				unset($this->session->data['last']);
				$this->response->redirect($this->url->link('account/login', 'inactive', true));
			}
			$this->session->data['last'] = time();
in the file catalog/controller/account/login.php
just before:

Code: Select all

		$this->response->setOutput($this->load->view('account/login', $data));
you can add:

Code: Select all

		if (isset($this->request->get['inactive'])) {
			$data['message'] = '<p><i>For your security, you have been Signed Out due to 20 min of inactivity</i></p>';
		} else {
			$data['message'] = '<p><i>For your security, you will be Signed Out after 20 min of inactivity</i></p>';
		}
you can later refer those messages to your language files.

in file catalog/view/theme/default/template/account/login.twig
you add wherever you want the message to show:

Code: Select all

{{ message }}

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Fri Nov 13, 2020 11:49 am
by sw!tch
nightwing wrote:
Fri Nov 13, 2020 10:43 am
How can we force a refresh at this point to redirect to logout page automatically?
Thanks
You handle a check via an event listener and Ajax .

FWIW - The header shouldn't really be responsible for handling this type of logic.

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Fri Nov 13, 2020 12:17 pm
by nightwing
Ok noted... I will do some research and implement.
sw!tch wrote:
Fri Nov 13, 2020 11:49 am
nightwing wrote:
Fri Nov 13, 2020 10:43 am
How can we force a refresh at this point to redirect to logout page automatically?
Thanks
You handle a check via an event listener and Ajax .

FWIW - The header shouldn't really be responsible for handling this type of logic.

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Fri Nov 13, 2020 12:32 pm
by straightlight
sw!tch wrote:
Fri Nov 13, 2020 11:49 am
nightwing wrote:
Fri Nov 13, 2020 10:43 am
How can we force a refresh at this point to redirect to logout page automatically?
Thanks
You handle a check via an event listener and Ajax .

FWIW - The header shouldn't really be responsible for handling this type of logic.
+ 1

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Fri Oct 28, 2022 6:56 am
by gitbro
Anyone know how to to make this refresh the page auto in stead of waiting for the customer to return and refresh before it logs them out.

Thanks.

Re: How can I logout OpenCart 3 customer after 30 minutes of inactivity?

Posted: Fri Oct 28, 2022 8:59 pm
by straightlight
gitbro wrote:
Fri Oct 28, 2022 6:56 am
Anyone know how to to make this refresh the page auto in stead of waiting for the customer to return and refresh before it logs them out.

Thanks.
With jQuery, this can be done. Look it up on Google.