Hi there!
First I want to say a big thanks for the developers of US. It is a great framework!
I have one question: is there any internal variable and/or function which could help me implement the following:
I want to limit the number of an user's simultaneous sessions to just 1(one).
Explaining: a user logged in from a "device one". If this user decides to log in using another "device two" without a previous log off from "pc one", I want to close the session in "pc one" automatically.
PS: the idea of forbid the user to login another session before his previous session be closed isn't practical.
Thank you all for the great job.
Demilson
Dear Brandin,
I just upgraded to the most recent version: 4.3.23 . Everything went well during the updated process.
Now, I'm back thinking how to limit the user's logged session number to just 1.
I just found the custom_login_script.php and seems to be a good place to put something like this:
$db->query("UPDATE us_user_sessions SET UserSessionEnded=1,UserSessionEnded_Time=NOW() WHERE UserSessionEnded=0 AND kUserSessionID <> ?",[$_SESSION['kUserSessionID']]);
What do you think?
Thank you!
Demilson
Looks good to me. Test it out-there is a chance it won't work since I do not recall if the Session is generated before or after the custom login script-we'll have to look into it.
Hi Brandin!
It seems I achieved the solution. Just added the following in the file custom_login_script.php and a user wont be able to login in more than one time simultaneously. To test: open a session in one device and, then, try to login another session (same user, of course) from another device or even from an browser private navigation window.
<?php
//Whatever you put here will happen after the username and password are verified and the user is "technically" logged in, but they have not yet been redirected to their starting page. This gives you access to all the user's data through $user->data()
$db->query("UPDATE us_user_sessions SET UserSessionEnded=1,UserSessionEnded_Time=NOW() WHERE UserSessionEnded=0 AND fkUserID=?", [$_SESSION['user']]);
Redirect::to($us_url_root.'users/account.php');
?>
Please, could you verify if the way I did to logoff previously opened session is "a nice one"?
Thank you!
Demilson
PS: I've just found the file oauth_success_redirect.php inside userc/includes . Should I use it instead of custom_login_script.php ?
Hi Demilson,
Thanks for your reply. Glad to here it worked.
Can you please elaborate on the "a nice one"? If you're just referencing the query, I would say it is fine.
Wise of the redirect, that is only a redirect for using FB and Google OAuth, which actually I don't even think is controlled from there anymore (I think thats just a failsafe maybe?)
Brandin.
Is session management turned on in the dashboard?
Sorry. It took me a second. Try this in usersc/scripts/custom_login_script.php
$sessions = fetchUserSessions();
if($sessions){
$count = count($sessions);
if($count > 1){
$sessions = array_reverse($sessions);
foreach($sessions as $k=>$v){
if($k != 0){
killSessions($sessions[$k]);
}
}
}
}