How to display users who is online?

How to display users who is online? There is a registration system how to make a seat


Answer 1, authority 100%

If you do not know the language, then freelancing is for you. If you know, then I will point you to the idea: to make a table with online users in the database. And when a user enters the site, fill in his id and time of visit to it, and each time he visits any page, update the time of the visit of the last one. Next, on the online list display page, display all users from the created table, for example, whose last visit was less than 2 minutes ago. If more, then delete this entry. If something is not clear, ask.

Well, first of all, creating a table would look like this:

CREATE TABLE online(
 idint(10) NOT NULL auto_increment,
 ipvarchar(20) NOT NULL default '0',
 iduservarchar(80) NOT NULL default '0',
 unixvarchar(60) NOT NULL default '',
 KEY id(id)
)

The code has not been checked, there will be errors, write – we will debug together
Now you need to do on all pages

include 'online.php';
 online();

But if you need to display a table of users online, then like this:

on_line(true); 

Here is the online.php code:

------ File online.php

/f-i counts users on the line; returns the number of users in formatted, i.e. to display the result, you just need to write in the right place like: echo on_line(); / function on_line($echousers = false) { $host = "localhost"; // host where the MySql database is located $db_name = ""; // database name; usually matches the username // (variable below), although I am strongly against the same // names, focusing on protection... $db_user = ""; // user who is allowed to access the database $db_password = ""; // user password $wine = 300; // online accuracy (seconds); the time during which // we consider the user who visited the page to be located // on the site $table_online = "online"; // table name // make the IP address global variable available global $REMOTE_ADDR; // connect to the MySQL server and select the desired database mysql_connect($host,$db_user,$db_password) or die(mysql_error()); mysql_select_db($db_name) or die(mysql_error());

// delete everyone who has already spent $wine seconds or who has the current IP $sql_update = "DELETE FROM $table_online WHERE unix+$wine < ".time(). " OR ip= '$_SERVER[REMOTE_ADDR]'"; $result_update = mysql_query($sql_update) or die(mysql_error());

// insert your entry $sql_insert = "INSERT INTO $table_online(id,ip,iduser, unix) VALUES ('','$_SERVER[REMOTE_ADDR]', '$_SESSION[userid]', '".time()."')"; $result_insert = mysql_query($sql_insert) or die(mysql_error());

// count uniques online $sql_sel = "SELECT idFROM $table_online"; $result_sel = mysql_query($sql_sel) or die(mysql_error());

$online_people = mysql_num_rows($result_sel); // number of On-Line users $online_people = (string) $online_people; // cast to string type // (this is how it should be.. see below)

$rain = strlen($online_people) - 1; // number of the last character in the number // on-line users

// output formatting (I did everything for you =) if($online_people[$rain]==2||$online_people[$rain]==3 ||$online_people[$rain]==4 ||(strlen($online_people)!=1&&$online_people[strlen($online_people)-2]!=1)) // $line - a variable that defines the output format $line = "person"; else $line = "person"; // return result if($echousers==false) Return "on the website & lt; strong & gt;" $ online_people. "& lt; / strong & gt; $ line"; ELSE { Echo "& lt; Table & gt;"; While ($ row_sel = mysql_fetch_array ($ result_sel)) { $ Res2 = MySQL_Query ("Select * from Users WHERE PK_USER = '$ ROW_SEL [IdUser]"); $ row2 = mysql_fetch_array ($ RES2); Echo "& lt; TR & GT; & lt; TD & GT;" $ ROW2 ['FLName']. "& LT; / TD & GT; & LT; / TR & GT;"; } Echo "& lt; / Table & gt;"; } }