开发者

How to know how many concurrent user using my application in php?

I have a php web page, but I would like to show how many user is c开发者_C百科urrently online.... How can I do so? Thank you.


Something to consider looking into is mod_status. It's an Apache module that creates a simple HTML page with details (one of which is number of active clients/requests). If you don't have access to a DB, or would prefer not to add the overhead of calling it each time the user requests a page, you could have PHP parse the status file created by mod_status for the number of active clients. For security, you might want to consider hiding the status file from normal users with some rewrite rules.


Counting the number of active sessions is probably the best way to do what you want.

You could use PHP's session_set_save_handler() function to set up some custom code that will run whenever a new user session is created or destroyed, and have that custom code maintain an application-scope variable that stores the current count of active sessions.

http://www.php.net/manual/en/function.session-set-save-handler.php

http://www.leosingleton.com/projects/code/phpapp/


The first two solutions would even be real-time. My preference would be number 2, because then I have most control. The order in which I would implement are 2, 1, 3, 4.

1) Hosted solution Pusher

The best solutions would be to use efficient transport(like for example long-polling or websockets) which can keep connection open. Sadly this has scaling problems when using PHP/Apache. Luckily you can use hosted solutions like Pusher. Pusher also offers a free plan, but has a concurrent connection limit of 20. This means you can not use this solution for free if you think you will have more than 20 concurrent users online at the same time. If not you could use presence events to detect number of users efficiently.

2) Host yourself using socket.io

Host it yourself using socket.io. For this you will need to compile node.js and redis and use redis client library. It would be pretty straightforward to implement this in socket.io. But keep in mind your count can be off if users open multiple tabs/windows to your page and you don't detect this. On connection event join room count for example just sent along session_id() which is unique per browser(session). You should store this session_id in your database. I would be using Redis because it is insane fast and easy to use. Store number inside redis set using sadd. If sadd returns 1 then you know that session does not yet have a connection. If not then there is already a connection to socket.io in different tab/window and you could just disconnect that connection. You should also get the number of session_ids using scard and sent them back to all connected users using socket.broadcast.to('count').send('<<< scard from redis >>>');

3) Use redis only

P.S: I am out of time, and would like to improve answer when I have some more time.

When not using efficient transport the number will be off a little bit. But this is how I would implement this. add session_id() to set using sadd. Also put time() in key with session_id() to keep track of when user connected. If I was more than take for example 3 minutes you know the session is not longer connected. To keep session live you post session_id() to server every two minutes. Be aware that if user opens more windows/tabs you will have to process more requests, but you could keep track of the number of open windows using cookies. You will also have to keep track of expired sessions and delete them.

4) Use your database(PDO)

Right now I am out of time to answer this, but will update this.


You'll have to save last visit of unique IP visitor. (If you would like to take it by IP)

After that, select everyone that last visited your site in past 100 seconds (example).

$query = "SELECT count(1) FROM unique_users WHERE last_click > ".(time() - 100);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜