Good night HeshKod, there was a great difficulty, I make a chat on Ajax and I have to delete those who have not been active for 5 minutes, here is the php code.

$date = strtotime("+5 minutes"); mysql_query("DELETE FROM tb_online WHERE date < $date"); $sql = mysql_query("SELECT * FROM tb_online WHERE id_session = '$id_session'"); if(mysql_num_rows($sql) > 0){ mysql_query("UPDATE tb_online SET date = '" .time() ."' WHERE id_session = '$id_session'"); }else{ if(isset($_SESSION["username"])){ $username = $_SESSION["username"]; mysql_query("INSERT INTO tb_online(id_session, username,date)VALUES('$id_session', '$username','" .time() ."');") or die(mysql_error()); }else{ mysql_query("INSERT INTO tb_online(id_session, username,date)VALUES('$id_session', 'Гость','" .time() ."');") or die(mysql_error()); } } 

Here is the Ajax code:

 function getOnline(){ $.post("data.php", {data:"get"}, function(text){ $("#user").html(text); setTimeout(getOnline(), 1000); }); } 

For some reason, they are permanently deleted and re-inserted (they are constantly flashing), why are they removed if the code seems to be correct, it should not be deleted immediately, and if 5 minutes was not active? What is the problem?

    2 answers 2

    and what do you want? your client receives the data and inserts it in the right place $ ("# user"). html (text) ;. Therefore, they will first be deleted and then appear

    If you want to actually make sure that only new users are added and inactive users are deleted, then you and the client should store a list of active users in the array. when you get a list from the server, then compare 2 arrays, if the user has gorged, then delete the args of the array and from the view, if the user has arrived, add it to the array and to the view.

    Also, the server part advice - write it down using caching. You can write yourself. The principle of the next-list of active users is not taken from the base but from the prepared file, the creation mark is also indicated in it, and the clients also send the mark of the previously received file to the request - if the tags match then you don’t send anything back - but here again is needed. But the file itself is overwritten from actions in the database - the user came - the file was overwritten, the user left - the file was overwritten.

      Maybe something is wrong with time .. look at the variables. Maybe you have a mistake in the numbers.