Delete line in html file

Asked

Viewed 126 times

0

I have a file that shows the time and name that a user logged in to the site, this file is password protected, is there any way to put a [Delete] button to delete certain line ?

I use the following code

 $f = fopen("pass/index.html", "a");
    fwrite ($f,
    'Usuario: [<b><font color="#660000">'.$usuario.'</font></b>]
    IP: [<b><font color="#996600">'.$ip.'</font></b>]
    Data: [<b><font color="#FF6633">'.$data.'</font></b>]<br> ');

    fclose($f);

On file it looks like this:

 Usuario: [<b><font color="#660000">teste1</font></b>] IP: [<b><font color="#996600">xxx.xxx.xxx.xxx</font></b>] Data: [<b><font color="#FF6633">07-07-2017 - 13:20:01</font></b>]<br>

User: [teste1] IP: [xxx.xxx.xxx.xxx] Date: [07-07-2017 - 13:20:01]

I would like after the time to have a [Delete] to remove that line.

  • Ever thought of using Sqlite?

  • I’m using a free lodging, not possible

  • Try this: https://github.com/bluelovers/txtSQL

  • Actually wanted the button to be in the html file itself

  • 1

    PHP has nothing to do with HTML, PHP is back-end and HTML is front-end, you will use txtSQL on the back and you will create a button on the front (html) that will send a request to the back-end (php) and PHP will run txtSQL and voilà.

  • free hosting with php, who is?

  • Actually in the file it is like this User: [<b><font color="#660000">teste1</font></b>] IP: [<b><font color="#996600">xxx.xxx.xxx.xxx</font></b>] Date: [<b><font color="#F6633">07-07-2017 - 13:20:01</font></b>]<br>

  • It would be interesting to post the HTML of the page because it could make it much easier to search for this line

  • Explain these files better. A file that saves the data in another file? Or is it just a file for everything?

  • The login page saves the time and number of users when they connect, I want to put a "boot" [Delete] affix each line to delete that line

  • @Leocaracciolo These are two files, one that takes the username and date and saves the data in an HTML file. this HTML file is used as "log", but expensive that for each information has a button to delete a line of information .

  • Apparently the HTML is composed only with these lines and nothing else. See the answer.

Show 7 more comments

1 answer

0

//parte do código que exclui uma linha caso seja passado parâmetro via get
if (isset($_GET['num'])) {

  $num = $_GET["num"];

  // lê todo o conteudo do arquivo para o vetor linhas
  $linhas = file("arquivo.html");

  //retira do vetor a linha excluida o -1 é para a linha anterior
  unset($linhas[$num-1]);

  //cria o arquivo novamente
  $arq = fopen("arquivo.html", "w");

  // insere todos os elementos do vetor sem o excluido
  foreach ($linhas as $conteudo){
    fwrite($arq, $conteudo);
  }
  fclose($arq);

}else{

  $ip= $_SERVER['REMOTE_ADDR'];
  $data = date("Y-m-d H:i:s");

  $file="arquivo.html";

  //verifica se o arquivo existe
  if (file_exists($file)) {

     //conta numero de linhas para obter o número que servira de parâmetro no link excluir 
     $numLinhas = 0;
     $handle = fopen($file, "r");
     while(!feof($handle)){
       $line = fgets($handle);
       $numLinhas++;
     }
     fclose($handle);

   }

   $conteudo = "Usuario: [<b><font color=\"#660000\">$usuario</font></b>]IP: [<b><font color=\"#996600\">$ip</font></b>]Data: [<b><font color=\"#FF6633\">$data</font></b>] <a href=_altera_linha_arquivo.php?num=".$numLinhas.">Excluir</a><br>".Chr(10)."";

   //Escreve o conteúdo da variavel acima no arquivo
   file_put_contents("arquivo.html",$conteudo, FILE_APPEND);

}

You can test here. Every refresh a new line is created with the proper deletion link

Browser other questions tagged

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