LogOut after x minutes Idel

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
AhmedBR
AppGini Super Hero
AppGini Super Hero
Posts: 327
Joined: 2013-09-19 10:23

LogOut after x minutes Idel

Post by AhmedBR » 2022-10-25 10:04

Automatic logout when idle after 30 minutes, checked once a minute.

Add the following to header-extras.php

Code: Select all

<script type="text/javascript">
             var IDLE_TIMEOUT = 30; //minutes
                var _idleSecondsCounter = 0;
                document.onclick = function() {
                _idleSecondsCounter = 0;
                };
                document.onmousemove = function() {
                _idleSecondsCounter = 0;
                };
                document.onkeypress = function() {
                _idleSecondsCounter = 0;
                };
                window.setInterval(CheckIdleTime, 60000);

                function CheckIdleTime() {
                _idleSecondsCounter++;         

                console.log(IDLE_TIMEOUT*2);
                if (_idleSecondsCounter >= IDLE_TIMEOUT*2) {
                alert("Time expired!");
                document.location.href = "../index.php?signOut=1";
                }
                }
</script>
Seems to be working well, so far.

Feel free to edit, change or suggest a better solution.
Have a nice day
AppGini 22.14 - xampp 3.3.0 - PHP 7.4.30 - Summary reports - Calendar - Mass update - Messages - AppGiniHelper

User avatar
jmcgov
Veteran Member
Posts: 81
Joined: 2018-12-19 01:31
Location: Northern Ireland

Re: LogOut after x minutes Idel

Post by jmcgov » 2023-01-25 22:52

I like that, thanks for sharing

apirnar
Veteran Member
Posts: 40
Joined: 2013-04-15 17:06

Re: LogOut after x minutes Idel

Post by apirnar » 2023-03-02 22:15

The timer works fine, but it doesn't redirect to the correct location to log off and gives "404 Not Found The requested URL was not found on this server." as well as not logging out since it didn't execute the signout.

Should the below line be modified or something else?

document.location.href = "../index.php?signOut=1";

apirnar
Veteran Member
Posts: 40
Joined: 2013-04-15 17:06

Re: LogOut after x minutes Idel

Post by apirnar » 2023-03-02 23:02

So I figured out what I was doing wrong.
document.location.href refers to the URL but my system is installed in a subdirectory so the path needed that specified.
The correct version is something like

document.location.href = "../MYINSTALLATIONDIRECTORY/index.php?signOut=1";

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1823
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: LogOut after x minutes Idel

Post by jsetzer » 2023-03-03 06:57

document.location.href = "../index.php?signOut=1";
If your page is located in root-directory of your app (actually your sub-directory), what about just using "index.php" (without "../"):

Code: Select all

document.location.href = 'index.php?signOut=1';
../MYINSTALLATIONDIRECTORY/
Caution
If you want to move your app to a different server and/or subdirectory at some point, remember that you will then have to adjust all such path information.

It is more foresighted to work with relative paths or to let the system give you the correct path (see next post).
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1823
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: LogOut after x minutes Idel

Post by jsetzer » 2023-03-03 07:07

Hopefully helpful information

I know this thread is about URLs in Javascript. But just in case someone needs to find out and work with paths in PHP, there are some helpful AppGini-PHP-functions in incFunctions.php:
  • application_uri()
  • application_url()
For example if your app in located in a subdirectory ...

C:\xampp\htdocs\app\crm\

... and you call the functions in a hooks-file ...

C:\xampp\htdocs\app\crm\hooks\footer-extras.php

... which is included in a normal TableView or in any custom page...

http://localhost/app/crm/TABLENAME_view.php
http://localhost/app/crm/custom_page.php

Code: Select all

C:\
└── xampp
    └── htdocs
        └── app
            └── crm
                ├── hooks
                │   └── footer-extras.php
                ├── custom_page.php
                └── TABLENAME_view.php
... then those functions will still return the correct relative directories and URLs:
chrome_CnvMvZndlh.png
chrome_CnvMvZndlh.png (8.31 KiB) Viewed 1725 times
Your can test it by yourself:

Code: Select all

<!-- file: hooks/footer-extras.php -->
<div class="well">
    <ul class="list-group">
        <li class="list-group-item"><small>application_uri()</small><br /><?= application_uri() ?></li>
        <li class="list-group-item"><small>application_uri('custom_page.php')</small><br /><?= application_uri('custom_page.php') ?></li>
        <li class="list-group-item"><small>application_url()</small><br /><?= application_url() ?></li>
        <li class="list-group-item"><small>application_url('custom_page.php')</small><br /><?= application_url('custom_page.php') ?></li>
    </ul>
</div>
I hope this helps. And of course you can use those functions in PHP and pass the results over to Javascript for later use.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

Post Reply