2
Well I’m creating a div
which is updated every x seconds and I am displaying the users currently logged in, but my code is inserting every update the same record. Is there any way to delete repeated lines?
Follow the code I’m currently using:
<?php
$id_usuario = $_SESSION['user_id'];
$usuario = $_SESSION['user_name'];
//grava os dados no arquivo
$arquivo = fopen("usuariosonline.txt", "a");
fwrite($arquivo, "<p><img src='avatar/".$id_usuario.".jpg'/>".$usuario."</p>\n");
fclose($arquivo);
// pronto aki terminou de adicionar
$file_name = "usuariosonline.txt";
// transforma as linhas do arquivo em arrays
$lines = file($file_name);
// verifica se a linha não é um comentário (apenas se você queira adicionar comentários no file.txt)
foreach($lines as $key => $value){
if(substr_count($lines, "#") == 0){
$texts[] = trim($value);
}
}
// retira as linhas duplicadas (se não for utilizar os comentário no file.txt, utilize a variável $lines)
array_unique($lines);
//abre o arquivo e o reescreve (se você não quer modificar o arquivo só ignora os códigos abaixo)
$file = fopen($file_name, "w");
fwrite($file, implode("\n", $texts));
fclose($file);
include('usuariosonline.txt');
?>
Still appeared the Warning:
Warning: substr_count() expects Parameter 1 to be string, array Given in line 19
Now the question became more objective. Note: this -1 was not mine, since you improved the question.
– user28595
You can set to generate a cookie when the user logs in. Then you set to only re-enter if the cookie no longer exists. If you want the code, you can talk and change your topic by saying that you would also like this code to avoid confusion
– Victor Eyer
Boy as to cookies I do not understand anything, but I have this doubt also in php, I create another question better? what question should I ask in this case?
– Arsom Nolasco
You would only set a cookie when the user enters the page for the first time. So you could put to write his name in the file of online users only once. You can also set a time for this cookie to expire. It’s very simple. I recommended to change the topic, because anything that is not in agreement here they already give negative vote and are filling the bag.
– Victor Eyer
I will try to create a new question, as the answers I’m on hj even made a vent on one of the issues that posted more worthwhile
– Arsom Nolasco
@Victoreyer I think you should rethink your words. Here is no place for mess. If you do not like how it works, you are not required to use. If you have any constructive criticism or suggestion, do it on [meta] and the community will say or disagree with you. As for the issue of the question, it ends up disturbing the answer that has already been posted. The issues should be elaborated properly, to avoid this kind of thing.
– Bacco
I talked about that kind of answer too
– Arsom Nolasco
@Victoreyer when you say "they already vote negative" you are excluding yourself. The community is all of us, you included :) If there are injustices sends a warning to moderation. If we separate between "we" and "they" we will all learn less about programming.
– Sergio