Page 1 of 1
Members online
Posted: Mon Sep 27, 2010 1:10 pm
by tonchily
Okay, so I got a table users which consists of the following rows:
id
username
password
status
I made a php page which checks for users account credentials, and if they're all matching, his status row is updated to "ON" (which means he's online and he's added to the "Who is online" box on homepage). After user clicks the "Logout" button, status row is updated to "OFF". But what if user just closes the browser without clicking the Logout button first? How to make it automatically status row update to OFF? Or is there any better way to accomplish my goal?
Cheers
Re: Members online
Posted: Mon Sep 27, 2010 1:19 pm
by jabbaonthedais
What I would do is have a timestamp field in your table like "last_online". When you're quering to find who's "online" just pull where timestamp is within the last 5 minutes. That way you don't have to rely on any action from the user. You would update the "last_online" column each page load.
Re: Members online
Posted: Mon Sep 27, 2010 2:44 pm
by DigitalMind
Add AJAX functionality to your project.
Add one more field `last_activity`timestamp not null default current_timestamp.
Write a script which will be update the field. Something like:
<?
session_start();
if (isset($_SESSION['id']) {
$db = new mysqli($host, $user, $password, $dbname);
$stmt = $db->prepare('update accounts set last_activity = now() where id = ?');
$stmt->bind->param('i', $_SESSION['id']);
$stmt->execute();
}
?>
add js code to your pages (use JQuery):
$(document).ready(init);
function init() {
$users_online_scheduler = window.setInterval(usersOnline, 50000); // every 50 seconds
usersOnline();
}
function usersOnline() {
$.post('usersonline.php', {'}, function (data) {});
}
Enjoy your online status

P.S. in order to get online users list use something like:
select * from accounts where last_activity > date_sub(now(), interval 1 minute);
Re: Members online
Posted: Mon Sep 27, 2010 10:35 pm
by Jonah Bron
Both provided solutions are good, but the first is a must. You can implement the second one, but do it on top of the first as a fall-back if the user doesn't have Javascript enabled.
Re: Members online
Posted: Tue Sep 28, 2010 4:01 am
by tonchily
Thanks, got it to work.
Uhm, I got another question:
lets say I want to make a php script that will echo "the newest member is $newestmember" where $newestmember is account that most recently registered.
Just don't have an idea on how to do it.
Cheers
Re: Members online
Posted: Tue Sep 28, 2010 4:16 am
by DigitalMind
select max(id) as newestmember from accounts
Re: Members online
Posted: Tue Sep 28, 2010 4:28 am
by tonchily
DigitalMind wrote:select max(id) as newestmember from accounts
Right. Thanks a lot guys!
edit: Help!
<?php
require 'connect.php';
$sql = "SELECT id, username FROM users";
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
$id = $row['id'];
$username = $row['username'];
$newestmember = max($id);
echo $newestmember;
?>
This is what I get
Warning: max() [function.max]: When only one parameter is given, it must be an array in C:\xampp\htdocs\bfp\newestmember.php on line 10
.
How do I make all id rows as array?
Re: Members online
Posted: Tue Sep 28, 2010 11:35 am
by Jonah Bron
Add "ORDER BY id DESC LIMIT 1" to the query, and skip the max().
Re: Members online
Posted: Tue Sep 28, 2010 2:15 pm
by tonchily
Jonah Bron wrote:Add "ORDER BY id DESC LIMIT 1" to the query, and skip the max().
Good thinking there. Thanks.
Re: Members online
Posted: Tue Sep 28, 2010 3:07 pm
by DigitalMind
tonchily wrote:$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
$id = $row['id'];
$username = $row['username'];
$newestmember = max($id);
Oh my godness
Re: Members online
Posted: Tue Sep 28, 2010 3:31 pm
by tonchily
DigitalMind wrote:tonchily wrote:$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
$id = $row['id'];
$username = $row['username'];
$newestmember = max($id);
Oh my godness
Yes?
Re: Members online
Posted: Tue Sep 28, 2010 3:48 pm
by DigitalMind
Yes. Try to understand what your code does. You're fetching an only row and trying to get max value of one value like max(10).
I wrote you above to use select max(id) from users. Feel free to execute a few queries for different results. Let's say execute first query for user list and second for finding the newest user. It's very simple. Just try to understand how it works. Also fetch results in a loop!
Re: Members online
Posted: Tue Sep 28, 2010 3:55 pm
by DigitalMind
BTW in this case
Jonah Bron wrote:Add "ORDER BY id DESC LIMIT 1" to the query, and skip the max()
is absolutelly right solution, at least if you need not just last id. But anyway you should fetch results in a loop or inside "if" statment because it returns nothing if the table is empty.
Re: Members online
Posted: Tue Sep 28, 2010 4:10 pm
by tonchily
DigitalMind wrote:BTW in this case
Jonah Bron wrote:Add "ORDER BY id DESC LIMIT 1" to the query, and skip the max()
is absolutelly right solution, at least if you need not just last id. But anyway you should fetch results in a loop or inside "if" statment because it returns nothing if the table is empty.
Yup, got it now to work both ways. Thanks.