I wonder if this is a good solution for a medium size social site. Its about managing online/offline indications of users. My solution is to include a script on each page that first of all updates the "last activity" field of the current user with a timestamp, then i select all users that havent been active within the session expire time(but is still online accordingly to the table) and sets the online flag(in db) to false. Is this a solution that is commonly used or will the sever load be to much when a bigger amount of users are navigating throug the site? Is there any better solution? Be aware that im not only intrested in the amount of online users but also names, therefore a session based script seems to complicated and inaccurate.
Thanks in advance!
It is good enough but better you could store this information in activity table: for any active user insert/update a row with his user_id and for inactive for more than N minutes one - remove rows.
A table that stored sessions should contain a field called "LAST_ACTIVITY_DATE", TIMESTAMP. So whenever they load a new page or you update something with AJAX, just update LAST_ACTIVITY_DATE with current_timestamp.
Then when you need to get online users, get those who have last_activity_date within 5 minutes from Current_timestamp.
Easy as!
Related
I'll try to explain my question the best way I can.
I'm not asking for codes, only for the best method of doing it.
I want to create a browser game and use time for upgrading stuff, building etc.
For example, to build 1 house will take 1 hour.
So I will began with saving the timestamp+(60*60) at the moment the user did his action.
My question is, how to update it the best way?
One way I thought of was to add function that check every page view of the user if it's done.
But then if he's not logged in the update wont happen.
Second way i thought about is for every page view of any user to check for every user registered. But it's not effective and there is a problem if no user is logged in.
Any suggestions?
I had my game doing it simply, without crons.
When a player wanted something that takes time, i just updated his database information with the appropriate time of ending that job (columns are just examples)
UPDATE player SET jobend = UNIX_TIMESTAMP() + (60*60*4) # ending in 4 hours
Then, every page that had an information about the remaining time, i just used something like this:
SELECT (jobend - UNIX_TIMESTAMP()) AS jobremaining FROM player
I formatted correctly the time using strftime and i displayed that to the user.
In the case the remaining time was negative, the job was done.
There was no-need for absolute counting since user was able to do something with the job when he was connected.
When the player just changed pages or doing something else i had a function where i just checked all timely events while the user was online (so to catch any negative timer), then presented with javascript any change (i posted javascript counters for every page)
Now, if you talk about updating in real-time, cron is the way but are you sure you're going to need it for a game? I asked that question myself too and the answer was not.
EDIT
If another player sees the buildings on schedule page (an hypothetical page) i am doing the same calculations; if a time just got negative for a specific player (regardless if another player see the page), i just reward him with the building (in database i make all the changes), even if he's offline. There's no harm in this, since he can't do anything anyway. The other players will just see he has a building. The key here is that i execute the required updating PHP code regardless of player's connection to the game; as long at least ONE player is logged-in i'm executing the progress function for everything.
This isn't so slow as it sounds (updating all players by using just a connected player that visits a specific page). You just have a table of 'jobs' and check timers against the current time. More like a single query of getting the negative ones.
Ok so I store the User ID and a PHP Timestamp on a db. What I want to do is check whether the user has visited on a daily basis. Something very similar to SO's visited system. I know that I should have the user's visit record.
But what I can't imagine right now is how do I determine the days that the user has visited and not visited. Any ideas? I really can't piece it together some help would be appreciated. A simple point to the direction if its ok. thanks
Edit:
I want to know the days the user has visited. Literally if the user visited on Nov. 1, 2013. I want to identify that but it wouldn't be just one day it would be each day since the user registered.
Append your user table with the following columns:
visittime
visitcount
consecutive (boolean)
When a user visits, check if the visittime was more than a day ago, and update consecutive accordingly. Als increment visitcount if visited consecutively.
There are a couple of approaches to solve this problem.
The most common way is to create a new table in your database.
This table should contain an "access log" of yout users (on entry per user per day). This way you can check if the user has accessed the site or not on a particular day. With this information you can calculate (almost) any information regarding to access stuff.
The fields coud be:
user_id
visit_time
visit_count
consecutive (tiny int)
You could add those fields to your existing table as well, but I think an additional table is cleaner.
This should get you up and running I guess.
So I suppose your main problem is how to determine if visitor is new or not.
You can use a $_COOKIE for that (as google analytic do), incrementing it for each visitor (this will be your visitor-unique-id)
Then if the cookie and you is not present, this is a new visitor.
Then store every page loading you want in a db
I currently maintain a DB table of users, when after logging in I update the table with their ID and login_time. This works to a point but currently I can't tell if the user has been active since the login or for how long.
Is there a better way to get a complete list of users that have been active in the past X minutes?
The best way to get what you need would be a "Last Activity" column in the users table. You would just update it whenever a user access a page. Depending on what information you need it could replace the login_time column or it could be a new column.
You'll have to keep track of when the user made their last request in your database as a separate table or column. You can then formulate a query to select, e.g. all users that have made a request in the last 5 minutes.
PHP itself does not store - or care for - that kind of information. Unless you happen to have your own session management module which does store this kind of information, then you could use data from that.
I run a local directory website (think yelp/yell.com etc) and need to provide analytical data to the businesses listed on the site.
I need to track the following:
1) Number of visitors to specific pages (ie: Jim's widgets was viewed 65 times)
2) Number of times a user clicks a link (ie: 25 users clicked to visit your website)
I am able to do this by simply adding one to the relevant number every time an action occurs.
What I would like to be able to do is split this into date ranges, for example, last 30 days, last 12 months, all time.
How do I store this data in the database? I only need the theory, not the code! If someone can explain the best way to store this information, I would be extremely grateful.
For example, do I use one table for dates, one for the pages/links and another for the user data (links clicked/pages visited)? The only solution I have so far is to add a new row to the DB every time one of these actions happens, which isn't going to scale very well.
Thanks to anyone that can help.
I would not reinvent the wheel and use an already available solutions such as Piwik. It can actually read your normal weblogs to provide all the information you asked for.
If for some reason you still need a custom solution, I would not save the tracking data in ranges, but rather use exact time and url-data for each individual page call (what your normal weblog provides). The cumulated data should be generated on-the-fly in your logic section, e.g. through a SQL-view:
SELECT count(url),url
FROM calllog
WHERE calldate > NOW()-30days
I'm currently looking for the BEST and SMOOTHEST way to get the active users on my website using a particular page. (purpose: think game lobby).
I was thinking about storing each visitor in a table, with their IP-adress as primary key in the database, together with a lastAction field - which stores a timestamp of their last action.
Gathering the active users would then simply just be to gather all rows where the last action was within the correct amount of time.
The problem: people on the same network (same IP) would be identified as the same user - it'd be hard to tell if they actually were a real person or just a page refresh with an old session. You get the idea.
I'm looking for smoother ways to separate users, or preferably, get away from the whole IP-adress part. Any ideas or suggestions would be appreciated.
Why not use a session based algorithm to determine the count of 'active users'? If you setup your session handler to write to a database (instead of flatfile), you can run queries against rows within the table to determine how many visitors and which page they are currently viewing.
You can read about how to switch your session handler here, but it does appear to be outdated. I suspect more googling will result in newer implementations.