开发者

Who's Online Session Stats

I have a MySQL table called "sessions", that contains a session_id, user_id, ip_address, and last_updated field.

I have the following questions:

  1. What's the best way of updating the "sessions" table if the user has not visited the site before, or if the user navigates to a different page?

  2. If a users navigates to a page that requires them to be logged in, what's the best way of remembering the page that they were viewing, prior to them being redirected to 开发者_运维问答the login page, so that they can be returned to it after logging in?

  3. How do I calculate the total number of members/guests currently online, and the total members/guests for the day, even if users of the same computer use different web browsers?

  4. I want to display how many users/guests are viewing a certain page. What's the best way of keeping track on what page the user is currently on.

Any help is greatly appreciated. I've been scratching my head about the above for some time!!! ;)


What's the best way of updating the "sessions" table if the user has not visited the site before, or if the user navigates to a different page?

I'm not sure what's being asked here. How do you update the table now? What are you tracking in the cases you mention in this question? Whether they've visited the site before or not, they still have a session. And I would think that no matter what page they navigate to, you're using the same code to maintain session information, no?

If a users navigates to a page that requires them to be logged in, what's the best way of remembering the page that they were viewing, prior to them being redirected to the login page, so that they can be returned to it after logging in?

Don't remember it. Pass it as a parameter to the login page (on the query string, generally) and then on the login page check for that parameter and redirect the user to that page, or to a default page if no parameter is specified. You can use urlencode() to add the value to the query string (and urldecode() to retrieve it).

How do I calculate the total number of members/guests currently online, and the total members/guests for the day, even if users of the same computer use different web browsers?

If they have different web browsers they'll still have different sessions. How are you expiring data in your session tracking? To count the total number of current sessions you'd just do a SELECT COUNT(session_id) on that tracking table and filter for active sessions. Sessions have a nasty habit of being difficult to track expiration of, since users don't always actively tell the site that they're logging out or leaving.

Tracking the total sessions for the day is similar, you'd just use a different filter on the SQL query. I imagine that last_updated field is going to be the key to filtering for both of these queries, either filtering by a session timeout for active users (20 minutes?) or by date for users for the day.

I want to display how many users/guests are viewing a certain page. What's the best way of keeping track on what page the user is currently on.

I guess you can add a current_page column (or whatever you want to call it) to your tracking table to update it with the user's current page each time you update their session activity. (Again, you re-use the same session tracking code on each page, right?) That way any time a user visits a page on your site, you record it.

You'd still have the same issue with session timeouts whereby if somebody just walks away or closes their browser then you'd show them as being on that page until their session expires or they fall outside of your filter (20 minutes?), but that's to be expected.


You can calculate when users are online based on a timestamp of the last request. I used UNIX timestamps in my Social networking site.


  1. any way that suit you. Personlly I prefer SQL query
  2. Show login form in place, with no redirects
    • a trivial SQL query with count(*)
    • noway with session stats table
    • doesn't matter. 2 browsers means 2 clients
  3. Another field in your sessions table.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜