The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automatic logout all userspice pages when one of them is logged out
#11
Yeah that was understood. Thank you so much!
  Reply
#12
Okay, I just about have something, but it won't be the prettiest of solutions I'm sure, but it would work as a starting point that you could refine.

*edit* I do have something that will work, I just need to write up the instructions for you.
  Reply
#13
Awesome. Thank you so much!

Best,
Angel
  Reply
#14
So, here is the javascript you can paste in the footer with the rest of the script tags (or wherever it will get executed on everypage...maybe you have a .js file you are already including or something):

http://pastebin.com/CiqVKuW0

And here is a PHP file you need to create and call users/helpers/logoutcheck.php (you can change the name, you can see the reference in the JavaScript code...just make sure they match). Also, you can set the check interval by replacing 5000 with something else (that is the time interval in milliseconds....5000 milliseconds = 5 seconds.

http://pastebin.com/9D5DKkiB

In your code you will need to change US_URL_ROOT to $us_url_root (I'm working on UserSpice 5 code base so that's why it is different).

Lastly, you need to create a hidden div or span that contains this text (again you can change it as you want, as long as the references all match) "Not Logged In" when $user->isLoggedIn()==False, and displays "Logged In" when the user is logged in. The HTML ID of this div or span needs to be set to "loginstatus" so the jQuery can find the right text and pass it to the PHP page.

Now, I'm not a JavaScript person, but this is how I solved the problem. The javascript function runs every 5 seconds, and when it runs, it executes the logoutcheck.php file with the string contents of the "loginstatus" hidden div. Inside the PHP file, it checks if $user->isLoggedIn() is false AND the string does not say "Not Logged In". If this is the situation, it means the user session has ended on another page, but the current page still says the user is "Logged In". This means the state of the page does not match the $user session state and it will run a redirect to the homepage ($us_url_root.'index.php'). Any other combination of states and it won't do anything.

This is a really crappy description and for that I apologize. See where you can get, and we can go from there.
  Reply
#15
Oh wow thanks for the detailed explanation! I'll give it a try now and let you know how it goes.

Thanks!
Angel Smile
  Reply
#16
I will see about adding a more robust version for either 4.2 release or 5.0 release.
  Reply
#17
hi Brian,

I just added the js code at the bottom of header.php and created the logoutcheck.php in the helpers folder. The html div is also created with the following code in header.php:
<div id = "loginstatus" style = "display: none" >
<?php
if ($user->isLoggedIn()==False){
echo "Not Logged In";
}else{
echo "Logged In";
}
?>
</div>

I notice that when the javascript post data to logoutcheck.php, there is an error saying:
Access forbidden!

You don't have permission to access the requested object. It is either read-protected or not readable by the server.

If you think this is a server error, please contact the webmaster.

Error 403

Is there anything wrong with my implementation?

Thanks
Angel
  Reply
#18
Delete the .htaccess file (for now) that is in the users/helpers folder. That should make it work. There is probably a better place to put that file, but you can move it (and adjust the JS reference to it anytime you wish).
  Reply
#19
it works! thanks a lot!

an issue I see is that somehow my page gets refreshed every 5 seconds. Is that a supposed behavior?

In addition, I changed one phrase in your code to be:
$.post(<?=$us_url_root?>+"users/helpers/logoutcheck.php", {loginstatus:$("#loginstatus").text()},
instead of this:
$.post("users/helpers/logoutcheck.php", {loginstatus:$("#loginstatus").text()},

I noticed this because I had all my pages under usersc folder instead of users folder. so when I'm at my pages the setInterval function gets redirected to users/users/helpers/logoutcheck.php instead of users/helpers/logoutcheck.php. Please let me know if this is a proper fix.
  Reply
#20
I guess PHP would parse $us_url_root prior to displaying the page, so if it works for you, then that is just fine.

The page should only refresh in the case where the user is logged out, and that div doesn't say "Not Logged In". So it *should* only fire on the pages which meet that criteria, and shouldn't happen at all when a user is logged in.

Just to confirm, if you sign in, then open another tab for your userspice install, and then sign out of one of your tabs...the tab you didn't sign out of should be the only one that refreshes.

However, you are seeing each page reload and take you to the destination specified in the logoutcheck.php?
  Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)