Members online

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Members online

Post 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
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Re: Members online

Post 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.
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: Members online

Post 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);
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Members online

Post 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.
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Re: Members online

Post 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
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: Members online

Post by DigitalMind »

select max(id) as newestmember from accounts
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Re: Members online

Post by tonchily »

DigitalMind wrote:select max(id) as newestmember from accounts
Right. Thanks a lot guys!

edit: Help! :oops:

<?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?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Members online

Post by Jonah Bron »

Add "ORDER BY id DESC LIMIT 1" to the query, and skip the max().
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Re: Members online

Post by tonchily »

Jonah Bron wrote:Add "ORDER BY id DESC LIMIT 1" to the query, and skip the max().
Good thinking there. Thanks.
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: Members online

Post 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
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Re: Members online

Post 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?
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: Members online

Post 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!
User avatar
DigitalMind
Forum Contributor
Posts: 152
Joined: Mon Sep 27, 2010 2:27 am
Location: Ukraine, Kharkov

Re: Members online

Post 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.
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Re: Members online

Post 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.
Post Reply