I have to restrict users in opening multiple tabs of any browser for security purposes like bank. I want user to perform their tasks in single Tabs of browser only.
If they try to open multiple tabs then their user IDs must be forcefully log out.
I don't know how to do this.
Please help me out or provide me some logic s to do this.
Thanks in advance.
First, I have to agree with the other users -- if someone having more than one tab open is a security issue, you have problems. Big problems.
However, if you still really want to do this:
When the user logs in:
// Must be a string
var dateTimeStamp = "" +new Date().getTime();
// This one can be seen by all windows/tabs
localStorage.setItem("timeKey", dateTimeStamp);
// This one can only be seen by the current window/tab
sessionStorage.setItem("timeKey", dateTimeStamp);
Now, when a page loads, just check to see if those two values match:
var localKey = localStorage.getItem("timeKey");
var sessionKey = sessionStorage.getItem("timeKey");
// If there is a key:
if (localKey) {
if (localKey !== sessionKey) {
// Handle multiple login here? Redirect to login page?
// Complain about tab?
}
}
Then, when the user logs out, make sure that you remove both of those keys.
localStorage.removeItem("timeKey");
sessionStorage.removeItem("timeKey");
Warning:
This kind of coding is bit fragile and no doubt there is an edge case I missed.
Extra Credit
It is possible to monitor changes to localStorage
and force all the other pages to log out if someone logs in on a new tab.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.