How to show online users in PHP?

PHP - User online script

  • How do I check whether the user is online or not? How do I show online users in php? Can anybody help? It's urgent please.

  • Answer:

    // 1. $ip = $_SERVER["REMOTE_ADDR"]; $min = 15; $time = time(); $timeout = $time - ($min * 60); // 2. mysql_query("DELETE FROM online WHERE ip = '$ip' OR timestamp < $timeout"); mysql_query("INSERT INTO online(ip,timestamp) VALUES('$ip', $time)"); // 3. $sql = mysql_query("SELECT COUNT(0) FROM online"); $online = mysql_fetch_array($sql); $online = $online[0];

Gaurav Srivastava at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Build a table where you can update a timestamp when a logged in user doing some activity. You know how large is your session lifetime value so when an activity happens, you'll need to check 'dead' sessions (delete those rows) and display how many 'active' sessions you've got. Note: this won't be an exact value

fabrik

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.