SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Hyderabad
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry How to restrict the user not close the browser with out signout?

    Hi

    i have designed one application where the user has to sign out compulsary.

    so i want restrict the user not to close the browser with out the signout.
    for this i written the code on the window.onbeforeunload. but whats happening here is that even when the user refreshes the page also the the alert is showing. or even when the user clicks on the browser back button also.

    i want to display alert when the user trying to close the window. if the user refreshes or clicks on the browser back button, the alert should not be fired.

    how can i do this?

    thanks
    Srinivas


  2. #2
    SitePoint Addict
    Join Date
    Nov 2003
    Location
    Malmoe, Sweden
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You canīt. The browser is installed on the client computer and is therefore under total control of the user sitting in front of that computer. You should design your application by these rules and not trying to rule over them. Itīs like the old saying: The customer is always right (or always has the final word anyway). Users (client computers) are like customers and your application is served by a web server (you see: server == serve).

  3. #3
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by srinu
    Hi

    i have designed one application where the user has to sign out compulsary.

    so i want restrict the user not to close the browser with out the signout.
    for this i written the code on the window.onbeforeunload. but whats happening here is that even when the user refreshes the page also the the alert is showing. or even when the user clicks on the browser back button also.

    i want to display alert when the user trying to close the window. if the user refreshes or clicks on the browser back button, the alert should not be fired.

    how can i do this?

    thanks
    Srinivas

    I would hate that as a user
    Now if you want to do it go to some porn sites and steal their code I didn't remember their names but each time I fall on them accidentally I have to reboot my machine as I am attacked by their never ending window

  4. #4
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just let the session time out after x minutes?

  5. #5
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Hyderabad
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Quote Originally Posted by Icheb
    Why not just let the session time out after x minutes?
    i can do that. but for all the users who registered into my site i am giving 100 hrs access to my site. so i need to calculate the time, how much time they have been viewed my site. so the user has to signout compulsary. otherwise the time will be continue...the application viewed time will be expiry.

    so i need to restrict theuser to logout compulsary. so that i can maaintain the user visited time.

  6. #6
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    hope this helps...

    Are you using a server side language as well? As in asp you can use the global.asa to do this.

    basically each page your visitor visits it will look at this page, and run the scripts.

    so when the user logs in and the session("user") has a value, it will take the time and save it as a session. Also it will take the amount of hours the user has left and tell the server that this user can stay connected for that amount of time.

    When the user leaves, it will take another time and compare them. It will then subtract that amount of time from the users.

    When the user returns, if they have no time left it will redirect to another page.


    Code:
    <script language="vbscript" runat="server">
    sub subtractusertime(uid,time)
    
    dim connection
    set connection = server.createobject("adodb.connection")
    connection.open "dsnname"
    
    dim recordset
    set recordset = server.createobject("adodb.recordset")
    recordset.open "select * from users where ID = '" & uid & "'", connection, 2, 2
    
    if recordset("userhours") > 0 then
    
    recordset("userhours") = recordset("userhours") - time
    recordset.update
    
    else
    
    response.redirect "nohoursleft.asp"
    
    end if
    
    end sub
    sub Application_OnStart
    
    end sub
    sub Application_OnEnd
    
    end sub
    sub Session_OnStart
    
    session("starttime") = time()
    
    if session("user") <> "" then
    
    dim connection
    set connection = server.createobject("adodb.connection")
    connection.open "dsnname"
    
    dim recordset
    set recordset = server.createobject("adodb.recordset")
    recordset.open "select * from users where ID = '" & Session("user") & "'", connection, 2, 2
    
    Session.Timeout=recordset("userhours")
    
    end if
    
    end sub
    sub Session_OnEnd
    
    session("endtime") = time()
    UserTime = DateDiff("h",Session("starttime"),Session("endtime"))
    
    if UserTime > 0 then
    
    call subtractusertime(Session("User"),UserTime)
    
    end if
    
    end sub
    </script>

  7. #7
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also have a little piece of java script running on the page that polls the server every minute.

    dont use some bloody piece of onclose event. That would get any user up in arms.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com


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
  •