Show users online

Asked

Viewed 1,434 times

2

Good people I’m wanting to make a system where shows online users at the moment but they are displayed more without restrictions, someone gives me a light

$timestamp=time(); 
$timeout=time()-300;    
$result = mysql_query("UPDATE novo_usuarios SET timestamp='$timestamp' WHERE ID='$id_user'");
$result = mysql_query("SELECT * FROM novo_usuarios WHERE timestamp<$timeout ");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

here comes the echo

  • Users have to be authenticated to appear, right?

  • yes , on the site will only view the content of the logged in users

  • then creates a table with the user id and status. Each time the user logs in, the state changes. To see that you are logged in, just make a query to this table

  • Yes, I know how to do that, but what is the problem? theoretically it was to list only those that the timestamp and less than 5 minutes am I right? I want to know the pq is not listing OBS: the update is working perfect , records in the field the team, what is picking and that at the time of displaying it is displaying everything as if it had not the condition of WHERE

  • Check this line, the skins $result = mysql_query("SELECT * FROM new_users WHERE timestamp < '$timeout'");

  • It really wasn’t that either

Show 1 more comment

2 answers

6


Just use the mysql time functions itself. You don’t even need to calculate the time in PHP:

$result = mysql_query("UPDATE novo_usuario SET validade=ADDTIME(NOW(),'0:05:00') WHERE ID=$id_user");
$result = mysql_query("SELECT * FROM novo_usuario WHERE validade>NOW()");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
...

Remember to use a column of the type datetime (or even timestamp) to store the time. I suggest not to use timestamp as a field name, not to be confused with the reserved word timestamp.

Explanation of the solution:

  • in 1to query, we updated the field validade of the current user with the date and time added to 5 minutes, using the functions NOW() and ADDTIME() of Mysql itself (NOW()can be replaced by the internal variable CURRENT_TIMESTAMP if you prefer).

  • in 2to query, we compare the field validade with the current moment. If these 5 minutes have not yet passed, the comparison will be true, and the user in question included in the results.

2

I believe this solves only what you need, not the problem itself.

$timestamp=time(); 
$timeout=time()-300; 
$result = mysql_query("UPDATE novo_usuarios SET timestamp='$timestamp' WHERE ID='$id_user'");
$result = mysql_query("SELECT * FROM novo_usuarios WHERE ".intval(timestamp)." < ".intval($timeout));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.