Whats the easiest way to determine if a user is online? (PHP/MYSQL)

前端 未结 10 1230
轻奢々
轻奢々 2020-12-02 05:24

Is there a way I can piggy back sessions to know if the user is online?

I.e: use logs on, I set a $_SESSION variable, user times out- cookie Garbage collector update

10条回答
  •  春和景丽
    2020-12-02 05:52

    Depends on your situation, it may be better for you to create a separate table "whose-online" with the columns: "ip" and "last-updated-time" and query/update this table every time a user loads a page.

    On page load queries may include:

    1. Update/insert "whose-online" table for current user based on ip.
    2. Delete "expired" rows (can also be done periodically using cronjob).
    3. Count "active" users.

    Benefits of using this technique:

    1. If a user is not logged in, he/she is still being counted/tracked.
    2. Depends on the amount of users you have, querying this table may be quicker.

    Note: If you use this you should want to take into consideration that any pageview will create a row in your table so based on useragent you can disregard bots or only count the popular ones (Firefox, IE, Safari, Opera, etc).

提交回复
热议问题