Finding total number of active sessions

前端 未结 5 1015
孤独总比滥情好
孤独总比滥情好 2020-12-18 10:28

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 b

相关标签:
5条回答
  • 2020-12-18 11:14

    This will count the amount of sessions open at once.

    $number_of_users = count(scandir(ini_get("session.save_path")));
    
    0 讨论(0)
  • 2020-12-18 11:15

    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.

    0 讨论(0)
  • 2020-12-18 11:16

    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)
    
    0 讨论(0)
  • 2020-12-18 11:19

    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;
    
    0 讨论(0)
  • 2020-12-18 11:28

    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.

    0 讨论(0)
提交回复
热议问题