Detecting online users: PHP
I am trying to make a thing where it says "X number 开发者_C百科of users are currently viewing this page" via PHP. How would I do this? Thank you.
There's no concept of "currently viewing" on the web, because everything happens as non-persistent connections. The closest you can get is "X users viewed this page during the last N seconds" (and if you choose a value of N that's small enough, you could say that it's pretty much equivalent).
Then, to have user A view data based on user B's actions, in PHP, you need to have some persistent storage, usually as a database like MySQL (but memcached or xcache could work as well, if you don't really worry about losing the data if the server crashes).
Simply write items such as "user X viewed page Y at time Z" to your persistent storage, and when page Y is displayed, query the storage for all views of page Y in the last N seconds, and count the number of different users.
In SQL:
INSERT INTO visits (user, page, time) VALUES (?,?,NOW())
SELECT COUNT(DISTINCT user) FROM visits
WHERE page = ? AND time < NOW() - INTERVAL 10 SECONDS
You could have the user tell the server "I'm still viewing" by submitting an AJAX request regularly (or having a small iframe
on your page that reloads itself every few seconds without JavaScript).
- Create a database table that stores:
- A hash of user IP and Useragent combination
- Timestamp
- Upon each pageload, store hash of user IP and Useragent combination along with current timestamp into database
- Query database for number of unique entries that have timestamp that falls into NOW() - X minutes range
- Display approximate number of active users
Expand on the basic idea by
- Having an interval spawned AJAX call to a script that updates db to account for users staying on a page for longer than X minutes
There is no failsafe way of doing this, but there are a couple of options.
- Save the last time the person visited in the database. If it's within a certain threshold (say 5 minutes), they're online.
- This way is less reliable, but occasionally ping ( correct term? ) the server with AJAX to tell it you're still online. This allows the user to have a page up for a long amount of time without their "online"-ness being timed out.
You could even use a combination of the two.
If you have a lot of traffic, you should cache the first option. You wouldn't want to run SELECT * FROM users WHERE lastvisit > UNIX_TIMESTAMP() - 18000
for every page load. Another option would be to run a daemon for that every minute or so, and store the online users in a separate table.
Note: this assumes you really meant users when you said it. If you want the number of guests online, see @code_burgar's answer.
You really can't because you have no idea when someone has stopped viewing a page.
Create a session variable with time:
$_SESSION['time']=time();
$time=time();
$query="INSERT INTO `online`(`name`, `time`)VALUES ('$name','$time')";
**// insert the time and username into the DB..**
if(time() - $_SESSION['time'] >=60)
**//check for ths condition if the user is available do the same thing again**
$_SESSION['time']=time();
$time=time();
$query="INSERT INTO `online`(`name`, `time`)VALUES ('$name','$time')";
While retrieving the online users:
$t=time();
// this will help u to remove the records that are not useful for u.
// **thus helping you to use less space on your database**
mysql_query("DELETE from online WHERE '$t'-time >60");
Finally, perform: mysql_query("SELECT * FROM online");
精彩评论