hey people. i am asking if it is posible to check for the user status using php. i mean to know if the user if online or offline.
i am trying to build a chatting system that alow me to know who is online and who is offline.

Dani AI

Generated

asked how to tell who is online. correctly pointed at WebSockets, showed a simple file approach, and urged a database + timeout. Two practical paths are common:

  1. Heartbeat (simplest for PHP): have a last_activity column and update it from the browser every N seconds. Treat any row with last_activity newer than your threshold (for example, 60–120s) as "online". This is reliable, works behind normal web servers, and is easy to secure.

Example schema:

CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(100) NOT NULL,
  last_activity DATETIME DEFAULT NULL
);

Heartbeat endpoint (PHP, using PDO):

<?php
session_start();
if (empty($_SESSION['user_id'])) { http_response_code(401); exit; }
$pdo = new PDO(...);
$stmt = $pdo->prepare('UPDATE users SET last_activity = NOW() WHERE id = ?');
$stmt->execute([(int)$_SESSION['user_id']]);
?>

Client ping (simple JS):

setInterval(function(){
  fetch('/heartbeat.php', { method: 'POST', credentials: 'include' }).catch(()=>{});
}, 30000); // every 30s

List online users:

$stmt = $pdo->prepare(
  "SELECT id, username FROM users WHERE last_activity > DATE_SUB(NOW(), INTERVAL 90 SECOND)"
);
$stmt->execute();
$online = $stmt->fetchAll(PDO::FETCH_ASSOC);
  1. WebSockets (recommended for full real-time chat): avoids polling and gives instant two-way updates, but requires a socket-capable server (persistent connections), more setup, and careful scaling. If a WebSocket demo (echo page) fails as @Member#120589 saw, common causes are: server not running, mixed-content (ws vs wss on HTTPS), proxy/firewall blocking, or incorrect endpoint.

Notes and cautions: always authenticate the heartbeat endpoint (session_start()), use prepared statements, pick heartbeat interval and threshold sensibly (threshold ~2x interval), and consider Redis/TTL for high scale to avoid constant DB writes.

Recommended Answers

All 6 Replies

i guess this is one of the things i want to know. but now i got another question which is how do i use this technology?
and i am asking how to know who is online like facebook.
you can see a list of the people who are online and chat with them. so this is what i am asking about

Member Avatar for Member #120589

websocket.org echo example does nothing for me. Says my browser supports it (chrome10 Win), but nothing happens on connect.

Hi

maybe you could try...

when the user logs in:

$user = "logged-in/$username";
$user = fopen($user, 'w') or die('ERROR');
fclose($page);

on logout:

unlink($username);

to list users:

$users = sort(glob('logged-in/*'));
foreach ($users as $user) {
  print $user; 
  print '<br />';
}

good luck :)

Calum

In order to track users, you'll need some sort of logon process. The previous post provides a low-end approach to doing this. This may satisfy your needs, otherwise you'll want to look at using a database to keep the user information, including their login status. You need a process to force a logoff for anyone who is inactive for a set period of time. Otherwise, everyone will eventually show as logged on / active.

good point chrishea; i'm not very familiar with mysql... lol, thats why my cms is flat file!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.