Finding total number of active sessions

14,004

Solution 1

I am improving the answer from @user2067005. You actually need to deduct 2 from the count because scandir reports the . and .. entries as well.

$number_of_users = count(scandir(ini_get("session.save_path"))) - 2;

Solution 2

All websites that allow you to show the number of visitors use database sessions handlers (it could be mysql db or even memcached).

Sessions table structure:

CREATE TABLE `sessions` (
`id` INT NOT NULL ,
`session_id` VARCHAR( 32 ) NOT NULL ,
`user_id` INT NOT NULL ,
`last_seen` DATETIME NOT NULL
) ENGINE = InnoDB;

Session ID could be either built-in php session id or your own unique hash. Built-in session works not so fast, but you don't need to watch for generating session ids, expiry dates and other.

User ID would be the current logged in user id, or NULL if he's a guest.

Every time user refreshes the page you should update your session table:

  • Update last_seen if session already exists in database
  • Add a new row for each new visitor that doesn't
  • Change user_id value if user logging in or logging out
  • remove expired sessions

At this point you can get all visitors count using simple sql query:

SELECT
    COUNT(`user_id`) AS users,
    (COUNT(*) - COUNT(`user_id`)) as guests
FROM
   `sessions`
WHERE
    `last_seen` >= DATE_SUB(NOW(), INTERVAL 15 MINUTE)

Solution 3

Yeah we can scan the temp/ folder OR session storage folder session.save_path for all session files but it always won't assure correct results some times even after session destruction session_destroy() session storage folder contains session files of logged out user. In this case you'd get wrong result. And also if you start phpmyadmin(MySQL) it'll create a session file of it's own.

Solution 4

This will count the amount of sessions open at once.

$number_of_users = count(scandir(ini_get("session.save_path")));

Solution 5

If you add/have a mysql table for statistics analysis which has at least ip address and time columns, even if your site has no membership activity, table will be populated by clicks of visitors.

In each of your page, insert ip and time to this table.

Then you can fetch distinct ip addresses clicked at most 15 minutes ago. Duration is strictly related your content, it may be even 10 secs or 1 hour. Ask yourself if you were your visitor, how much will you surf on your site and be honest of course:)

Lastly, your table will be fat after a time. Decide the frequency and logic and methodology to delete the useless rows. Clean the table on each arrival of your frequency which depends on your site traffic and sources.

Share:
14,004

Related videos on Youtube

Gowtham
Author by

Gowtham

Updated on June 04, 2022

Comments

  • Gowtham
    Gowtham about 2 years

    I'm a beginner to PHP and I'm writing some code to my site. I want to get the total number of sessions that is active at that instant. I knew this is some difficult task but possible. How can I do it?

    I've googled and some people say that it is possible by counting the total number of temporary session files in the temp directory. But, where is it located?

    For greater explanation consider the example of Joomla backend which shows the total number of current visitors and administrators as shown below:enter image description here

  • Gowtham
    Gowtham about 11 years
    That is returning a value 15 and for each session started it is incremented but how to clear up when the sessions are ended.
  • Gowtham
    Gowtham about 11 years
    Yeah you're almost got me right, but my site actually allows only anonymous users. Will it work?
  • ozahorulia
    ozahorulia about 11 years
    Yes. In this case you don't event need user_id row and the last query goes this: 'SELECT COUNT(*) as visitors FROM `sessions`