How can I know when a session has ended without the use of "session_unset"? (for exemple when a user close the current window but doesn't go to the log-out page)
| SitePoint Sponsor |

How can I know when a session has ended without the use of "session_unset"? (for exemple when a user close the current window but doesn't go to the log-out page)





Mabye something like this:
PHP Code:if(!session_id()){
// No Session
session_start();
}
else{
// Already a session
}
"A nerd who gets contacts
and a trendy hair cut is still a nerd"
- Stephen Colbert on Apple Users
When a user closes a window (and no other windows are open) a session ends. They don't have to go to the logout page for it to end.
ssegraves [at] gmail.com
On Image Use, Abuse, and Where We're Headed
stephan | XMLHttpRequest Basics
flickr | last.fm | Cogentas, LLC

But how can I use this information (session ending) to update something on a table database?




Assuming you have a $_SESSION set..
PHP Code:
if(!isset($_SESSION['whatever'])) {
// connect to the database then
$query = "UPDATE table SET blah='blah' WHERE blah='blah'";
$insert = mysql_query($query)or die(mysql_error());
}
If knowledge is power - Why isn't our army librarians?!
Statistics show that 63% of all statistics are fake.
When i was little i broke my neck, and i havent looked back since.
I completed the internet in 1 week. The end boss was pretty easy though.

But this way I can't know info from the session.
Let me explain better what I want. I wanna make a users online list. I was thinking of updating a users online table when someone logs in. But then, how can I update when he logs out, the way I said above?





I thought they did it with cron jobs.





on every page refreshing u see how many logged user u have!
if u want to see it in real time u have to take some php online users script and go to javascript session and then ask there how to update in real time a number with this script.

But there is no way for me to delete a user from the online users table when he logs out without hitting the Logout button, right?Originally Posted by reminder
How can I see the sessions created on the server and check which one is gone?
I'm kinda confused![]()





Ah, there is no way to do that. What things like Vbulliten do is periodically check to see if the user is online.
So in steps:
1. Add user and date/time of access to DB.
2. Everytime the user performs an action update the date/time.
3. Have a regular function to test if the date/time is older than a give value. Say an hour. If it is delete user from DB. He is no longer online.
"A nerd who gets contacts
and a trendy hair cut is still a nerd"
- Stephen Colbert on Apple Users

Yeah, I didn't think it was possible but I had to askOriginally Posted by The New Guy
Thanks!




Logout.php:
That could be a logout script. How to do what you do with when the browser closes etc, no, that isnt possiblePHP Code:session_start();
$destroy = session_destroy();
if($destroy) {
$query = "UPDATE users SET online='no' WHERE id='blah' and online='yes'";
$update = mysql_query($query)or die(mysql_error());
header("Location: wherever");
}
.
If knowledge is power - Why isn't our army librarians?!
Statistics show that 63% of all statistics are fake.
When i was little i broke my neck, and i havent looked back since.
I completed the internet in 1 week. The end boss was pretty easy though.
Yeah, I think he is wanting to know how he could do a logout when the browser is closed.
You could try some fancy Javascript and see if that works. When a user closes a window on your site, you open a very small one up, run your PHP and then close that window. It could work.
ssegraves [at] gmail.com
On Image Use, Abuse, and Where We're Headed
stephan | XMLHttpRequest Basics
flickr | last.fm | Cogentas, LLC



Originally Posted by Hartmann
However, that very well could be taken as a popup and killed by many different programs. >_>
"Sa souvraya niende misain ye."
- Robert Jordan, The Wheel of Time

Thanks everyone.
The JavaScript thing is not gonna happen and I'll just do it like VBulletin.
Thanks again for the help





u cant know when someone else close the browser window without logout!
but u can destroy the session from admin section.
example: create an admin section with select form which take all users from db and then do that script:
now in phpHTML Code:<select name="UserSession"><!-- php stuff --> <option value="<?php echo $row[1]; ?>"><?php echo $row[2]; ?></option><!-- end php stuff --> </select>
put this into a formPHP Code:$data = $_POST["UserSession"];
$query = mysql_query("select from user where id = ".$data);
while($row = mysql_fetch_rows($query)) {
$_SESSION["id"] = $row[1];
}
mysql_free_result($query);
and when u click the delete session submit button go on this function:
now your selected user havent a session.PHP Code:if(isset($_POST["your_submit_button"])) {
$_SESSION["id"] = 0;
}
hope this helps
cya
Bookmarks