online user check?? with php

Status
Not open for further replies.

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
does any one know how to check whether a user is online ,i mean the registered user,for example if a user is online in this forum a image just besides his pic appears green and if he is offline it appears dark blue or black.

can any one give me a hint how does the online stuff work....i downloaded a quiet a few scripts like smf,phpbb2 ...but those scripts are just too complicated for me .
so please help!! iam tryin to make my own custom scripts
 

worthynotes

New Member
Messages
5
Reaction score
0
Points
0
Just an idea, but you could set a cookie that expires when the user closes the browser/logs out etc, and if that cookie's there, they're online, and if they're not there, they're offline. Just one idea, I'm sure other people will have other ideas.
 

flinx

New Member
Messages
68
Reaction score
0
Points
0
You can't use cookies to see who's online and who isn't, since cookies are stored on the client computers, and not on the server.
You can make a new field in your users table, which contains the last action date/time. Every time the user does something, you update that field. To decide if a user is active, just confront the stored date/time with the current date/time.
 

Bobswat

New Member
Messages
104
Reaction score
0
Points
0
if you are using a mysql table to hold your users you could just have a field `is_online` and then just have it be set to bool (true or false) and have it react like that, then just use an if statement

PHP:
<?php
include('mysql_connection.php');
$result = mysql_query("SELECT * FROM `tablename` WHERE `username` = 'member_name' LIMIT 1");

while($row = mysql_fetch_array($result)){
$online = $row['is_online'];

if($online == "true"){
//insert green image
}else{
//insert purple or other image
}
}

I mean, it's sorta messy, and I'm sure there are better ways to do it, I would suggest using the cookie like worthynotes said, but i figured I would offer an alternative.

Good luck!
 

flinx

New Member
Messages
68
Reaction score
0
Points
0
A boolean isn't enough, because if a user closes his browser without loggin out, he'll stay 'online' in your database. You really need to use a timestamp and then check if he timed out.
 

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
thanks ! does any one know how these scripts like vbulletin show whether a person is online
 

Xemnas

New Member
Messages
812
Reaction score
0
Points
0
It's pretty basic - in one implementation, the user database table has two fields, one holding a Unix timestamp (the last time a user performed an action) and the other holding a boolean (whether a user is online). When the "online now" script is executed, it queries the database for all users marked as online, and checks their timestamps against the current Unix time. If any timestamps are from x or more seconds ago, those users are marked as offline, and the other users are then shown.

If you're looking for actual code, here's an example:
PHP:
<?
error_reporting(0);
$now=time();
$cutoff=time() - 600;
$query="SELECT field_un FROM bbg_accounts WHERE field_lastaction BETWEEN '$cutoff' AND '$now'";
$result=mysql_query($query);
$row=0;
do
{
   $name=mysql_result($result,$row);
   echo $name;
   $row++;
}
while(mysql_result($result,$row)!='');
?>
 
Last edited:
Status
Not open for further replies.
Top