Click to See Complete Forum and Search --> : PHP and Browser tracking


youssefe2k
05-12-2004, 09:30 PM
hi all,

i have 2 questions, any help would be appreciated:

1) is there a way to know when a user has closed his browser and to have the php script do something while the browser is closing? like to save information to a file or something like that?

2) concerning php sessions, does a session file get deleted when the u doe session_destroy() and when the user closes the browser?

youssefe2k
05-12-2004, 09:44 PM
hi one more question,

3) is there a way to access or see all the sessions that are active at any given moment?

bwkaz
05-12-2004, 10:05 PM
Originally posted by youssefe2k
1) is there a way to know when a user has closed his browser On the server, no, there is not. There is no difference on the server between a closed browser window and a user that's sitting on one page. Each HTTP request comes in on a new connection (unless you're using keep-alive, but that still doesn't keep state across requests on the same connection).

and to have the php script do something while the browser is closing? like to save information to a file or something like that? It may work to hook up a javascript function to window.onclose (or whatever it is), but I wouldn't rely on that function being able to actually do anything on the network (like post to a logout page). Likewise, don't rely on that function being able to open a new window -- any decent popup blocker will prohibit that.

Personally, I'd keep as much state as possible on the server, and then automatically save it whenever it changes (rather than relying on the client to do it).

As for 2, I have no idea. As for 3, I'm guessing yes. (Oh, you wanted to know what it was? Sorry, I have no idea. ;))

youssefe2k
05-12-2004, 11:17 PM
hi thanx for the reply,

i think i read somewhere that a session is destroyed when the user closes the browser window what i wanted to know is how it figures out that the browser windows gets closed? and yeah i wanted to know how to do it for 3) :) thanx anyways

nouse66
05-12-2004, 11:30 PM
Originally posted by youssefe2k
hi thanx for the reply,

i think i read somewhere that a session is destroyed when the user closes the browser window what i wanted to know is how it figures out that the browser windows gets closed? and yeah i wanted to know how to do it for 3) :) thanx anyways

i think its because the session cookie is set to expire when the broswer closes and the browser can just delete it then.

youssefe2k
05-13-2004, 12:23 AM
yeah but a session also writes a temp file on the server itself thats the difference bettween session cookies and php sessions ... the file contains session variables and so on ...

Suramya
05-13-2004, 02:44 AM
I think there is a function in Javascript called onPageUnload or something like that... You could try using that to call a .php page to process stuff.

youssefe2k
05-13-2004, 03:21 AM
it looks like nouse66 is rite about cookies my bad... looks like im going to do something in javascript unless someone knows how to do it in php, also does anyone know the answer to 3?

youssefe2k
05-13-2004, 03:30 AM
ps. does anyone know the javascript code for the onclose thing

bwkaz
05-13-2004, 07:10 PM
You can't rely on onclose being able to do anything, though.

The "cookie expiration" thing doesn't affect the server, either. When the cookie expires, the browser just stops sending it in HTTP requests (in the Cookie: header). The actual session, which is an entity on the server, always expires after a set timeout. If the server sees a request with a known session ID inside of that timeout, it resets the timeout. If the timeout elapses without a request being received with that session ID in its cookie, then the session ends. The browser does not end the session by exiting, but by not making subsequent requests with the session ID in a cookie.

What do you need to happen on the server when a session ends? Couldn't you just hook whatever function PHP runs that deletes the temporary file, and do it there? No, it won't happen immediately as soon as the browser closes, but that's impossible to do reliably anyway.

youssefe2k
05-13-2004, 07:49 PM
thanx for all replies,

i realized that the session does not get destroyed when the browser gets closed so i made an expirey thing:

i made the session have a time variable and if u visit a page after a certain time and u are still logged in it will boot u ... ie ill call session_destroy(); since i couldnt figure out how to call the funtion when window is closing ... thus the page will expire if u try to visit it after a certain amount of idle time ... but technically u are still logged in until u refresh the page ...

also does anyone know how to make dynamic text apear on a confirmation box? i mean a countdown b4 a value is automatically taken?