Hi,

I want to make a notifier for showing online users and number of room booked. But i don't know how to code it.

Can anybody help me?

Thanks in advance.

Dani AI

Generated

, a straightforward pattern is: record a last_seen timestamp per visitor, and expose a tiny JSON endpoint your page polls to update the notifier. This fits your PHP stack with a little JS (as @diafol hinted). Create a table like user_activity(session_id PK, user_id NULL, last_seen DATETIME). Make session_id unique and add an index on last_seen for fast range queries. Use PHP sessions to identify visitors (PHP sessions).

<?php
// notifier.php
session_start();

$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4','user','pass', [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$session = session_id();
$userId = $_SESSION['user_id'] ?? null;

// Upsert last_seen (requires UNIQUE/PK on session_id)
$stmt = $pdo->prepare(
  'INSERT INTO user_activity (session_id, user_id, last_seen)
   VALUES (?, ?, NOW())
   ON DUPLICATE KEY UPDATE user_id = ?, last_seen = NOW()'
);
$stmt->execute([$session, $userId, $userId]);

$online = (int)$pdo->query(
  "SELECT COUNT(*) FROM user_activity
   WHERE last_seen >= (NOW() - INTERVAL 5 MINUTE)"
)->fetchColumn();

// If one booking == one room; otherwise use SUM(room_qty)
$bookedToday = (int)$pdo->query(
  "SELECT COUNT(*) FROM bookings
   WHERE status = 'confirmed'
     AND created_at >= CURDATE()
     AND created_at < (CURDATE() + INTERVAL 1 DAY)"
)->fetchColumn();

header('Content-Type: application/json');
echo json_encode(['online' => $online, 'booked_today' => $bookedToday]);
// Poll every 10s and update your placeholders with ids "online" and "booked"
setInterval(() => {
  fetch('/notifier.php')
    .then(r => r.json())
    .then(d => {
      document.getElementById('online').textContent = d.online;
      document.getElementById('booked').textContent = d.booked_today;
    })
    .catch(console.error);
}, 10000);

Tips: keep the window (5 minutes) appropriate for your site, and only return aggregate counts (no personal data). Use prepared statements for writes (PDO prepared statements). Indexing last_seen matters for performance (MySQL CREATE INDEX). If you are not using jQuery, native fetch is fine (Using fetch). Adjust table/column names to match your schema.

Recommended Answers

All 2 Replies

Member Avatar for Member #120589

Have you searched Google? What have you found?
Are you aware that you need to use as js (probably jQuery), php, mysql?

i haven't found any direction on google.
My web's coded by PHP. In fact, i dont have any idea for it.

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.