Log user off web site when new user tries to log on

Hello! I am looking for some help with the following issue:

Imagine a wife has signed on to a web site in a browser tab. She does not sign off and walks away to do something else. Her husband sits at the computer and seeing her signed on, opens a new tab and tries to sign on to the same site.

I need to automatically sign the wife off when this happens so as to avoid confusion later regarding which session is active.

Is there a way to do this with javascript?

Thanks so much for any and all help.

The only way you could really do this is with Ajax and PHP sessions if your using PHP as the backend source. What you would need to do is set a session that stores the IP and some other information about the user logged in and then run that again the user trying to sign in.

If the IP doesn’t match and all the other information then let them continue otherwise stop the script and let the user know a user with the same IP is already logged in.

Hi! Thanks so much for your response.

What if I tried the following:

On the sign on page (or even better, the sign off page), when the sign on button is clicked, a piece of Javascript code is run first. The javascript simply goes through all tabs on the browser, and checks to see if any such tabs has an HTML document that came from the same domain as the sign on page. If such tabs exist, then before POSTing the user name and password to the server, the Javascript code will close those tabs that contain HTML documents came from the same domain.

So, I understand what the code to set the window.name to specific value would look like. But do you know what the code on the sign on/sign off page to close the other tabs/windows would look like?

Thanks again for your help. It is much appreciated.

By default you can’t close tabs in browsers that were not opened by javascript, currently only Firefox from what i have read has the ability to set a dummy window.open method which allows you to close the tab but thats as far as you can get.

To get a list of tabs you would need to build a browser extension for all browsers that gives you control over that and that for me would be a good learning experience but a huge time waster in my opinion.

Yep, I’ve been searching around and coming across the same issue.

Thanks so much for taking the time to respond. Much appreciated!

Hi, SgtLegend. I hope it’s ok to ask you directly, but I re-posted this issue elsewhere as a new scenario: What if I want to open multiple browser tabs and then close each of those tabs, either one by one or all at once?

Is there a way to do this?

For example, like this script:

The problem with that is that it lets you open more than one browser tab, but you can only close the first one you opened. I would like to close each of the new browser tabs opened with that script.

Thanks so much for any and all help.

Try the following below, i have tested it and it worked fine for me.

<!DOCTYPE html>
<title>Javascript new tab/window</title>
<meta charset="utf-8">
<script type="text/javascript">
    var windows = [], i = 0;
    function manageWindows(method) {
        switch(method) {
            case 'open':
                var url = prompt('Please enter a valid URL to open in a new tab/window', '');
                if (url) {
                    // Check if the URL contains "http://" in the string
                    // If it doesn't force "http://" to the front of the URL
                    if (!url.match(/^(http(s)?):\\/\\//i)) {
                        url = 'http://' + url;
                    // Make sure the url has a length of at least 1 or else
                    // do nothing
                    if (url.length) {
                        windows[i] = window.open(url, 'tabWindow_' + i);
                } else {
                    alert('No URL was entered');
            case 'close':
                var id = prompt('Please enter the id of the tab/window you would like to close', '');
                if (id) {
                    // Make sure the id is valid
                    if (typeof windows[id] === 'undefined') {
                        alert('You have entered an invalid tab/window id!');
                        return false;
                    delete windows[id];
                alert('Invalid method passed, must be either "open" or "close"');
        return false;

<a href="javascript:void(0)" onclick="manageWindows('open')">Create a new tab/window</a> &middot;
<a href="javascript:void(0)" onclick="manageWindows('close')">Close a tab/window</a> 


Hi! Thanks so much. This feels so close!

But I’m trying it in Firefox and Chrome and when I enter tabWindow_0 or tabWindow_1 or etc, it says I am entering an invalid id.

Is there a way to do this without all the prompts?

Here is a way to do it. The script stores the object references to the new windows in an array. These windows can then be closed by using
Make sure you are not blocking pop-ups, or you may not see the new windows as they are created.

<!doctype HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>



<meta http-equiv=“Content-Type” content=“text/html; charset=windows-1252”>
<title>Closing sub windows</title>
<script type=“text/javascript”>
var A=new Array(); // global
function newPg()
{ var nextW, i;
{ nextW=“a”+i;
A[i].document.body.innerHTML=“Page “+i;
// create links to new pages on parent page
var build=””;
for(i=1;i<4;i++){build+=‘<p class=“curs” onclick="A[’+i+'].close();this.style.visibility=\‘hidden\’">Close Page ‘+i+’<\/p>
'; }
// -----------
<style type=“text/css”>
body { font-family:arial,helvetica,sans-serif; font-weight:bold; color:#000; }
.curs { cursor:pointer; }




All you need to enter is a number, so if you have 4 tabs open and you only want to close tab 3 simply enter 3.

NOTE: Once a tab is closed the array key gets deleted so trying to the same ID will result in an invalid id message.

Thanks AllanP for the response.

And thanks SgtLegend for the clarification.