It is the most easiest to describe my problem with a working example: even if you are not logged in, YouTube remembers what you have watched, and next time gives you suggestions based on previous watched movies.
My site is similar in a way: the users can vote on articles without logging in, and the site remembers votes with cookies. I have figured out a working method, but there has to be an easier way - also now the DB usage is anything but optimized.
For every visitor there is a check if he has the cookies. If yes I query his votes. If not I create a dummy user, and send him out the cookies. Now I store this users "last_visit" timestamp. After this everything is the same for both users. My problem is that my DB is filling up with dummy users, so I made my cookies expire in 3 months and my site regularly check which users didn't visit my site in the last 3 months, and deletes them from the DB.
I know I overcomplicated this, but my vote system is using AJAX, and I couldn't find a method to send out a cookie (and create the dummy user) only if a vote happens and not every time a simple visitor browses my site - without vote.
Also a note: I insist on using cookies - I know it would be easier to store IP-s when a vote happens, but there are schools, businesses using the same IP, and I like to allow their users to use my site.
What did I miss here? How can this be optimized?
if they do not hold a permanent account, why store anything related to them in the database at all? just record their prior votes in the cookie. you would also store averall votes in the db, but anonymously, and not relate these to "users" at all.
Related
I am working on add to favorites add-on (where posts will be added to favorites by signed in users), but then I saw that craigslist lets anyone add favorites without them being signed in. How do they do that? Is there a unique $_SESSION id for a browser? Sorry if it's a silly question.
As an example: http://orlando.craigslist.org/search/jjj (Click on any star to add to favorites. May not be available in IE 11).
They are just using a session, you should be able to check this by looking what cookies they are storing on your computer.
Everything in $_SESSION persist only for the current session. If the users closes the browser and reopen it a new session is generated. So $_SESSION is not the right place for your answer.
There is $_ENV where you can get the clients ip address and general browser data from. In most cases this is enough to identify a single user. If you keep track of these data you can prevent users from giving multiple votes.
I have a question from a potential client (hence, no code yet) about a website they have which has a custom login script.
Basically, they have Analytics setup but it doesn't serve the purpose that they need - being able to tell how many times a user has logged in and how frequently they do so.
What would be the best way of achieving this? I'm guessing I would need to alter the PHP login script but I'm a little confused at how best to do it. I could do a new field in the database that counts their logins but I suspect that that's not a great way of doing it, and I'm not sure how I could tell the frequency from that type of system.
Any suggestions?
Every time a user logs in you could log it in your db or to a file. You could easily include user name, datetime, ip, user agent (all relevant data) etc. As mentioned this will make it very easy to build a profile on the user, login times, login locations etc
If you store the time of each attempt, you can run reports to figure out whatever you need.
the best way to implement this is a relational database. When a user is logged in, an entry is made in a table. When you want to find out how many times a user has logged in, a query to this table to count the entries that apply to that user.
You can simply make a new table where you store user_id and timestamp everytime the user is logging in. Using that data you can then see who, how many times and in which intervals of the day has been logged in.
I have a private web app with login requirements.
I have seen many sites (mainly forum sites) that list the members that are currently logged in and viewing the site.
I think I can devise a way to look at session vars datetime stamps and make some assumptions, but I was wondering if there was a standard way out there, or a trick that is used, to capture and display that info.
Not looking for code here (per se), just looking for the logic used to do it.
Thanks.
The most common approach is to just update a timestamp every time the user does something, and count her as logged out after a defined inactivity time.
You can use a session variable so you don't have to update the timestamp in the database on every page load. In this variable, keep a timestamp of the last update, and when it's over five minutes ago, update the timestamp in db and session.
You can't know when someone is watching your site, but you can determine when a user last requested a page from your server. If a user has requested a page in (for example) the last five minutes, you could say he's still active and probably looking at a page of your site. As far as I know that's the most common way to determine the number of active users.
How much granularity are you looking for? Do you only want to show users who recently logged into the site, or do you want to display users who are viewing (or recently viewed) a particular forum post?
If the former, it depends on what your login and authentication framework currently looks like, but basically it would be a query of non-expired sessions.
If the latter, you would need to store what users viewed a particular page when. You could use a join table such as Users_Viewed_Pages with a timestamp. Then for a particular page, query the table by Page_ID and display the users who loaded that page recently.
I'd suggest a binary value in your database and toggle it when the user signs on. Then you can have a JS timeout to automatically sign the user out if they're idle for too long.
Implement your own session handler and store active sessions in the database. This also makes it easy to terminate a user's active session and/or keep a log of all sessions.
I'm not sure what would be the easiest way to do this. I need to be able to detect what users are online on my website. So when people are viewing a thread or something it will say next to the users name if they are ONLINE or OFFLINE. And at bottom of forum index it will say all the users who are online.
What would be the easiest approach to do something like this? I'm not sure if I will need a Javascript which would run every time a page loads or what.
Thanks :)
have a MySQL database with a user table
have a column in that user table which saves the "last seen"-time
update that column on every page request
when checking for online or offline, check if current time minus "last seen"-time is greater than some treshold you like
Edit: You could optionally make a javascript "ping" the server (request an empty page) every two minutes or so if you want people idling with your Website open to be displayed as online, too.
One approach is to store your users sessions in a database or another store like memchached (or ideally both What is the best way to handle sessions for a PHP site on multiple hosts?). Then you just look up the user in your store and see if their session is still active.
A solution like this: http://pureform.wordpress.com/2009/04/08/memcache-mysql-php-session-handler/
You need to hold some kind of a "session" table, where you hold the user and the time of when they visited a page.
If the time is older then 5 minutes the user is offline (and the row can be deleted).
The other users in the session table are "online".
First thing first, there's no way to accurately get the current online users count. The best you can do is get a rough number.
Think about this situation: a user logs in to your website, clicks some links, and just closes the browser tab without logging out. Actually this is quite common. Your server never knows that a user closes the browser, so the session of that user keeps alive for some time (30 minutes maybe) if the session stored on your server.
In some stateless web apps like Ruby on Rails apps, sessions are stored completely on the browser side (in the cookies), and the server totally forgets about the user after each request, and that makes counting online users nearly impossible. This is a tradeoff for simplicity and scalability.
So how can we get such a number? We must make assumption basing on compromise. We make such an assumption that a user is online if his/her last request was made less than 30 minutes ago, then we can get the number of "online" users by tracking the timestamp of the last request each user makes. Or we can assume that a user is online if his/her session on the server is still alive, and count the sessions on the server. Either way, we have to convince ourselves that the number of "dangling sessions" is negligible (I know it's hard).
How would one go about implementing a "who's online" feature using PHP? Of course, it would involve using timestamps, and, after looking at phpBB's session table, might involve storing latest visits in a database.
Is this an efficient method, or are there better ways of implementing this idea?
Edit: I made this community wiki accidentally, because I was still new to Stack Overflow at the time.
Using a database to keep track of everyone who's logged in is pretty much the only way to do this.
What I would do is, insert a row with the user info and a timestamp into the table or when someone logs in, and update the timestamp every time there is activity with that user. And I would assume that all users who have had activity in the past 5 minutes are currently online.
Depending on the way you implement (and if you implement) sessions, you could use the same storage media to get the number of active users. For example if you use the file-based session model, simply scan the directory which contains the session files and return the number of session files. If you are using database to store session data, return the number of rows in the session table. Of course this is supposing that you are happy with the timeout value your session has (ie. if your session has a timeout of 30 minutes, you will get a list of active users in the last 30 minutes).
There are many ways of implementing this.
You can implement this simply by polling.
Check clients by certain interval and count the numbers of clients replied back.
That value can be used as number of online users.
I think better way is using push technology instead of checking online people certain every x seconds or x minutes.
It simply let server know when people are entering and leaving by event.
So then server just increase and decrease the the online user counting variable when events come from clients.
I recommend Socket.IO, APE to look at.
Also there are many other to consider such as XMPP, Jabber.
I was thinking to do it this way:
When the user logs in, his userid and timestamp will be inserted in to a table.
Then for every 5 minutes, i will call a php script via ajax to check whether the user is logged in, and if so, update his timestamp in the table.
If the user is not logged in, just delete his record.
i think you can make it simply in php
create table user consist of , username , password and status(1,0)
in login system analyse username and password
then if username and password is match , run the query to select from table user , and fetch it
then get the status
Update user , set status into 1 , it means online
When you click logout , analyse session username and password , and select query from tbl user and got the status , and then set status into 0 it means offline