For Ex:
There are 10 peoples who logged into the site. I need to track the name of the people who logged in at a admin side. Under what logic we can track the people. I prefer to do it on php. Please help me the logic of tracking.

Dani AI

Generated

spideyprasad: good question. and pointed in the right direction — sessions plus a database — but a practical admin-ready design needs two things: an audit of logins (who logged in and when) and a live-sessions feed (who is currently active). The audit answers "who logged in" and the session table answers "who is online now."

A compact schema that supports both (MySQL-style) looks like this:

CREATE TABLE login_history (
  id INT AUTO_INCREMENT PRIMARY KEY,
  user_id INT NOT NULL,
  session_id VARCHAR(128),
  ip VARCHAR(45),
  user_agent VARCHAR(255),
  login_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  logout_at DATETIME NULL
);

CREATE TABLE user_sessions (
  id INT AUTO_INCREMENT PRIMARY KEY,
  user_id INT NOT NULL,
  session_id VARCHAR(128) NOT NULL,
  last_activity DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  is_active TINYINT(1) NOT NULL DEFAULT 1
);

On successful login, insert a login_history row and create/insert a user_sessions row. Update last_activity on requests (or via a periodic AJAX heartbeat). To show "currently logged in" users, query sessions with last_activity within your threshold (for example 5–15 minutes):

SELECT u.username, s.last_activity
FROM users u
JOIN user_sessions s ON u.id = s.user_id
WHERE s.is_active = 1
  AND s.last_activity >= NOW() - INTERVAL 10 MINUTE;

Practical tips: avoid writing last_activity every single request — update only if older than N seconds, or use a JS heartbeat to reduce DB writes. Mark is_active = 0 on logout and have a cron job to clear stale sessions (older than your threshold). For security, always use HTTPS, set Secure/HttpOnly on cookies, regenerate session IDs at login, and log minimal PII. Finally, keep a short retention policy for logs and restrict the admin view to authorized accounts only.

Recommended Answers

All 2 Replies

You can use sessions and/or cookies to accomplish this. E.g:
1. When each user logs in, start a session containing their username.
2. Simply check the content of the session variables to determine who is logged in
3. Close their session when the user has logged out.

There is tons of info on this. I can help, but I'm not prepared to the write the code for you.

Basically what nonshatter said.

You'd need to create a database to store the sessions, users and last time that session was used.

If they're still logged in, and have used the site in the past XX minutes--you know they're probably logged in--you can do a simple SQL query to get their information.

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.