简体   繁体   中英

Finding total number of active sessions

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: 在此输入图像描述

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;

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)

这将计算一次打开的会话数量。

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM