SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  3. #3
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  5. #5
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, I've been searching around and coming across the same issue.

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

  6. #6
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:
    http://javascriptclosewindow.com/

    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.

  7. #7
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try the following below, i have tested it and it worked fine for me.

    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Javascript new tab/window</title>
    <meta charset="utf-8">
    <script type="text/javascript">
    //<![CDATA[
        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);
                            i++;
                        }
                    } else {
                        alert('No URL was entered');
                    }
                break;
                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;
                        }
                        
                        windows[id].close();
                        delete windows[id];
                    }
                break;
                default:
                    alert('Invalid method passed, must be either "open" or "close"');
            }
            
            return false;
        }
    //]]>
    </script>
    </head>
    <body>
    
    <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> 
    
    </body>
    </html>

  8. #8
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  9. #9
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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
    A[number].close();
    Make sure you are not blocking pop-ups, or you may not see the new windows as they are created.

    Code :
    <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     
    <html>
     
    <head>
     
    <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; 
        for(i=1;i<4;i++)
         { nextW="a"+i;
           A[i]=window.open("",nextW,"status,menubar,height=400,width=400");
           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>\n'; }   
        document.body.innerHTML=build;
        window.focus();
      }
    // -----------
    window.onload=newPg;  
    //-->
    </script>
    <style type="text/css">
    <!--
    body  { font-family:arial,helvetica,sans-serif; font-weight:bold; color:#000; }
    .curs { cursor:pointer; }
    -->
    </style>
    </head>
     
    <body>
     
    </body>
     
    </html>

  10. #10
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by meb71 View Post
    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?
    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.

  11. #11
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks AllanP for the response.

    And thanks SgtLegend for the clarification.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •