Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

execute DB code when browser closes?

Status
Not open for further replies.

converge

Programmer
Jul 10, 2002
15
US
I am designing a site where paying users can login to a searchable scientific database. But I only want them to be able to be logged in on one computer at a time. If a user clicks the logout button, I set a field in my table to LoggedIn = False. But what if they just close the browser? Then the database still thinks they're logged in. Is there a way for sessions to accomplish this task?
 
No. HTTP is a stateless protocol. There is no realtime connection between the two.

One workaround would be to use session variables. PHP's session handling mechanism uses cookies that are deleted when the use shuts down the browser. ______________________________________________________________________
My God! It's full of stars!
______________________________________________________________________
 
No. HTTP is a stateless protocol. There is no realtime connection between the two.

One workaround would be to use session variables. PHP's session handling mechanism uses cookies that are deleted when the use shuts down the browser. ______________________________________________________________________
My God! It's full of stars!
______________________________________________________________________
 
You could try tieing their login to the IP that they use, then if they try using a different ip with that same login within a certain time period, it would tell them so.

But, there is no method for detecting whether or not they close their browser.

Bring on flash/java, both of these can connect to a server for the duration of a visit.

Maybe you could code something with those --BB
 
you could use some javascript to submit a little hidden form that get ativated by the onUnLoad event in the body tag

<body onUnload=&quot;somefunction();&quot;>

then in the function just submit some small form item and use the server to update the db

hth
Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
you have to build your own session control.

see in the session section of the manual.

You can create a session management in the database, and then, when the user logins you create an entry for saving the session data and other information you need.

When you destroy the session you also delete the record or do what you want to say he's logged of.

You can also build the garbage_collect function to clean up junk.

But, there's one thing. Suppose that there's a IE crash and the browser closes (you MUST prevent this) you don't know that the user closed the window ... Well you can do another thing. If a user logins and closes the browser without logging out. If he logins again, let him go, but close his previous connection.

Using sessions, the user can open multiple windows with the same session_id if he opens by CTRL+N or file->new or anything similar, but if he opens a new instance of the browser the session_id will be different.

You can use cookies too. If a user closes the window, he can get back again and stay logged in. But it's not the best way.

Well, finaly my advise. Use a self session control to control the sessions, and if a user loggins twice, just log the first out automatically. If the old one is still in use, just send a message in the next page call saing &quot;Your session was terminated because you logged in a different location/browser.&quot;.

Hope this helps you out. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
i greatly appreciate all of your suggestions. i think i'm going to follow Anikin's advice and great my own session control. instead of the sessions using files, i'm going to overload the session functions to use the database.
 
It's not hard to create it. I had to create one for my website, cause it uses load-balancing. So i could not have sessions in the usual way, in files. I created a simple session management script to control my sessions in the database and to avoid problems in the load balancing.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
I did the same thing for the same reason. Making session variables able to survive load-balancing is, as Martha Stewart would say, &quot;A good thing.&quot;

I've posted a FAQ in this forum which describes how I did it. You might find it useful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top