Members online
Moderator: General Moderators
Members online
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
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
-
jabbaonthedais
- Forum Contributor
- Posts: 127
- Joined: Wed Aug 18, 2004 12:08 pm
Re: Members online
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.
- DigitalMind
- Forum Contributor
- Posts: 152
- Joined: Mon Sep 27, 2010 2:27 am
- Location: Ukraine, Kharkov
Re: Members online
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);
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);
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Members online
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
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
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
- DigitalMind
- Forum Contributor
- Posts: 152
- Joined: Mon Sep 27, 2010 2:27 am
- Location: Ukraine, Kharkov
Re: Members online
select max(id) as newestmember from accounts
Re: Members online
Right. Thanks a lot guys!DigitalMind wrote:select max(id) as newestmember from accounts
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?
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Members online
Add "ORDER BY id DESC LIMIT 1" to the query, and skip the max().
Re: Members online
Good thinking there. Thanks.Jonah Bron wrote:Add "ORDER BY id DESC LIMIT 1" to the query, and skip the max().
- DigitalMind
- Forum Contributor
- Posts: 152
- Joined: Mon Sep 27, 2010 2:27 am
- Location: Ukraine, Kharkov
Re: Members online
Oh my godnesstonchily wrote:$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
$id = $row['id'];
$username = $row['username'];
$newestmember = max($id);
Re: Members online
Yes?DigitalMind wrote:Oh my godnesstonchily wrote:$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
$id = $row['id'];
$username = $row['username'];
$newestmember = max($id);
- DigitalMind
- Forum Contributor
- Posts: 152
- Joined: Mon Sep 27, 2010 2:27 am
- Location: Ukraine, Kharkov
Re: Members online
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!
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!
- DigitalMind
- Forum Contributor
- Posts: 152
- Joined: Mon Sep 27, 2010 2:27 am
- Location: Ukraine, Kharkov
Re: Members online
BTW in this case
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.Jonah Bron wrote:Add "ORDER BY id DESC LIMIT 1" to the query, and skip the max()
Re: Members online
Yup, got it now to work both ways. Thanks.DigitalMind wrote:BTW in this caseis 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.Jonah Bron wrote:Add "ORDER BY id DESC LIMIT 1" to the query, and skip the max()