Page 1 of 1

LogOut after x minutes Idel

Posted: 2022-10-25 10:04
by AhmedBR
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

Re: LogOut after x minutes Idel

Posted: 2023-01-25 22:52
by jmcgov
I like that, thanks for sharing

Re: LogOut after x minutes Idel

Posted: 2023-03-02 22:15
by apirnar
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";

Re: LogOut after x minutes Idel

Posted: 2023-03-02 23:02
by apirnar
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";

Re: LogOut after x minutes Idel

Posted: 2023-03-03 06:57
by jsetzer
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).

Re: LogOut after x minutes Idel

Posted: 2023-03-03 07:07
by jsetzer
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 1601 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.