Click to See Complete Forum and Search --> : Ending a session when a window closes..
I was wondering how I could achieve this?
I want the session to end when the user closes the window.
The point is I have a "who is logged in: " thing on my site and I want the user to stay logged in when they close the window, but I want the session that controls whether or not they appear in the "who is logged in: " to end when they close the window.
Thanks :)
Not sure what you are asking. Sessions automatically end when the browser is closed. You must be writing the number of users to a file or DB? If so, you'll want to timestamp them, and then just check if they are older than X mins, and if so, delete them.
Oops forgot to mention that.
I am storing the session in a DB. So how would I go about doing that?
Just insert a timestamp when you add their name to the DB. Also, update the timestamp when users refresh the page(s) that keep track of who's online. If the timestamp get's older than the amount of time you want them to remain active, remove them.
Well I don't really want to put a time limit on it.
It is just that I am storing the online data in a database and when the user closes the browser, their name still appears in the bottom for being online, when they aren't online; they are just logged in.
Do I still use a timestamp?
Since PHP runs server-side, it won't be able to tell when users close their browser.
Is there anyway to get Javascript and PHP to interact?
Yeah, there are a few ways JavaScript and PHP can interact. JavaScript can pass variables to PHP through the query string, a form submission, or cookies. It can instruct PHP to run by using the location, and pointing it to a PHP page.
zachzach
11-10-2003, 05:36 PM
so you could do something like:
<body onunload="window.location='time.php?'+username">
or something like that...I dont know much about php but I know a bunch about javascript so I think that would work, just have the page name "time.php" remove the name "username"(just store a javascript variable of the user's username), and then have "time.php" close itself.
I certainly wouldn't recommend doing anything like that, but whatever...
Yea I really can't get this to work at all. I've tried a bunch of different things and just doesn't work :(
The problem is the user closes the window, ends the session, but still appears on the bottom :(
Ok here is what I have:
At the top of every page, it sets a cookie equal to yes.
$connect = mysql_connect("$localhost","$username", "$password")
or die("Could not connect: " . mysql_error()); //connecting to mySQL
$_SESSION['online'] = "no";// setting status to offline
if ($_COOKIE['cookie'] == "yes") // testing to see if user viewed this page
{
if (isset($_SESSION['username'])) // if username is set(loggedin)
{
$_SESSION['online'] = "yes"; // making online = yes
}
}
$check = mysql_db_query("dragon_main", "SELECT displayname FROM users WHERE online='yes'") // get all users online
or die("query error: " . mysql_error());
echo "Users currently online: ";
while ($row = mysql_fetch_array($check))
{
echo "<strong>".$row[0].","; // print out online users
}
$_COOKIE['cookie'] = "no"; // set cookie to no
?>
Here is where I made my sessions:
$lookUser = mysql_db_query("$database","SELECT displayname, encpassword FROM ".$table." WHERE displayname = '".$username."'")
or die(mysql_error());
$class = mysql_db_query("$database", "SELECT class FROM ".$table." WHERE displayname = '".$username."'")
or die("Error ".mysql_error());
$access = mysql_result($class,0);
while (list($user, $pass) = mysql_fetch_row($lookUser))
{
$_SESSION['username'] = $username;
$_SESSION['class'] = $access;
$_SESSION['lastpage'] = $lastpage;
$getthing = mysql_db_query("dragon_main","UPDATE users SET online='".$_COOKIE['cookie']."' WHERE displayname = '".$username."'")
or die(mysql_error());
$_SESSION['online'] = $getthing;
echo "You have successfully logged in: ".$user.". Taking you back to the last page you visited... <br><br>";
if (isset($_SESSION['username']))
{
if ($_SESSION['lastpage'] == "/changeclass.php")
{
echo "<META HTTP-EQUIV='refresh' content='1;URL=../index.php'>";
}
else
{
echo "<META HTTP-EQUIV='refresh' content='1;URL=..".$lastpage."'>";
}
}
else
{
echo "Error";
}
}
Sorry if it isn't very organized and hard to read :(
All in all, I'm trying to show Users online and if the user closes the page on, it logs him off.
DaiWelsh
11-11-2003, 10:14 AM
Originally posted by zachzach
<body onunload="window.location='time.php?'+username">
Think about that for a minute - I am a malicious website designer who wants to trap punters, all I have to do is use this to redirect to 'you cant leave' page and the poor user is stuck. You can do some things on unload but stopping them closing the browser by redirecting to another page is not one of them (and likely to really annoy your users if you do it when leaving the page without closing the browser too).
In theory you could open a fresh window to load your session end script, but again I would not recommend it myself, do the work your end, don't make the user suffer... :)
What are you talking about? ^^
DaiWelsh
11-11-2003, 11:19 AM
Did you miss the quote? I was refering to zachzach's post.
I agree 100% with DaiWelsh on this one...