need to add something to a table if the window is closed?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

need to add something to a table if the window is closed?

Post by me666 »

hey, i have a website that uses mysql and php so that users can login and sign up. wen a user logs in it adds an 'online' message to the table where there username is stored, and when they click logout it adds a 'offline' message. This all works fine no problem there, but i also have a rember me feature, if some uses it they dont logout, there for the table still has an 'online' message, and this also happens if they just close the window even if they havent used the remeber me feature... i wanted to add a code that will make the 'online' message go to 'offline' when they close the window... i understand that if this is posible it will probably be java but that isnt a problem as most of my site requires java anyway...
if any1 could help me with this i would be very pleased :)

thanks in advance, Leo

also while im here, can any1 tell me how to change the time zone in this code please?

Code: Select all

$todays_date = date('d/m/Y - h:ia');
when used with other bits of script it adds the date and time to my table, BUT it adds the tima as UTC... meaning the time is displayed an hour erlier than i want it to... i read up that u use the letter Z and some numbers representing seconds, but i couldnt figure it out... it just added a 0 for the Z and displayed the digits after the Z
Help on either 1 would be great :)
thanks :)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: need to add something to a table if the window is closed?

Post by califdon »

You will have to come up with an entirely different method of determining who is online, requiring rewriting of much of your application. The problem is that you are storing a value in the database that should be contained in a session variable. Since HTTP is a "stateless" protocol (there is no persistent "connection"), there isn't any sure way for a server to "know" when a browser client closes a window or just shuts down and goes to lunch. There are more complicated ways to determine this, such as polling every 30 seconds, or something, but I would say they are seldom justified. However, you can set a time limit on a session variable, and refresh it every 30 seconds, or something like that. The point is, you can't just "add a code" to your script and solve your design problem, you need to re-think the logic on which it is based.
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

Re: need to add something to a table if the window is closed?

Post by me666 »

ok that answers it well actually, basicaly u cant do what im asking for....well i suppose we all make mistakes ay.... well i use sessions for loggin in, but i used a tutorial to do that script and so im not to sure on how to use them to be honest.... this is the bit of script i use to add the online part... this is in a file called login.php...

Code: Select all

  $_POST['user'] = stripslashes($_POST['user']);
   $_SESSION['username'] = $_POST['user'];
   $_SESSION['password'] = $md5pass;
   $name = $_POST['user'];
   $todays_date = date('d/m/Y - h:ia');
mysql_query("update users set ip='$_SERVER[REMOTE_ADDR]' WHERE username = '$name'");
mysql_query("update users set laston='$todays_date' WHERE username = '$name'");
mysql_query("update users set active='Online' WHERE username = '$name'");
thats whatis set when some1 logs in... i have used the quiries to add the data when people log in... then in my files i have this code at the top of the page

Code: Select all

<? 
session_start(); 
include("database.php");
include("login.php");
$logged_in = checkLogin();
?>
so i can see how we would set the new variables so that people have 1 saying they are online once logged in, but im not sure how i would have all the names displayed on a page that are logged in... and the variables would be unset when they close the window, perfect, just the little matter on how to display all the online users now...
would be fab if u could help me :)
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

Re: need to add something to a table if the window is closed?

Post by me666 »

no1 have any idea then?
Post Reply