How to delete complete HTML content?

Asked

Viewed 239 times

0

I’m doing a chat using PHP, Javascript, Ajax and HTML. I know it’s not the best option, but I chose to create an html file log.html which stores all messages and is loaded by my PHP file index.php. All messages are sent to the log through the file post.php. I need to delete all messages, i.e., delete all the html content present in log.html at a certain time. How can I do this? Follow the file code.

Index.php

/* Tentando apagar o conteúdo do arquivo */
$var = date('H:i:s');
if ($var == '02:00:00') {
    $fp = fopen("log.html", 'w');
    fwrite($fp, "");
    fclose($fp);
}

<!-- Dentro do HTML da página -->
<div class="chatlogs">
    <!--    mensagens vem pra cá     -->
</div>
<!-- mensagem que será enviada -->
<form class="chat-form" name="message" action="">
    <textarea name="usermsg" id="usermsg"></textarea>
    <button name="submitmsg" type="submit" id="submitmsg"><i class="fa fa-paper-plane"></i></button>
</form>

// JS && Ajax da página
function loadLog() {
    var a = $(".chatlogs").attr("scrollHeight") - 20;
    $.ajax({
        url: "log.html",
        cache: !1,
        success: function(t) {
            $(".chatlogs").html(t);
            var o = $(".chatlogs").attr("scrollHeight") - 20;
            a < o && $(".chatlogs").animate({
                scrollTop: o
            }, "normal")
        }
    })
}
$(document).ready(function() {}), $("#submitmsg").click(function() {
    var t = $("#usermsg").val();
    return $.post("post.php", {
        text: t
    }), $("#usermsg").attr("value", ""), !1
}), setInterval(loadLog, 1e3);

Post.php

<?php
    /* Envio da mensagem para o log.html */
    session_start();
    if(isset($_SESSION['usuarioUsuario'])){
        $text = $_POST['text'];
        $fp = fopen("log.html", 'a');
        if ($_SESSION["usuarioNivel"] == 9) {
            fwrite($fp, "<div class='chat self'><div class='user-photo'><img src='../../dist/img/perfil/" . $_SESSION['usuarioImagem'] . "'></div><p class='chat-message'><span class='user'>~".$_SESSION['usuarioUsuario']."</span>".stripslashes(htmlspecialchars($text))." <span class='hora'>".date("g:i A")."</span><br></p></div>");
        } else {
            fwrite($fp, "<div class='chat friend'><div class='user-photo'><img src='../../dist/img/perfil/" . $_SESSION['usuarioImagem'] . "'></div><p class='chat-message'><span class='user'>~".$_SESSION['usuarioUsuario']."</span>".stripslashes(htmlspecialchars($text))." <span class='hora'>".date("g:i A")."</span><br></p></div>");
        }
    fclose($fp);
    }
?>

How I erase all the contents of log.html at a certain time without needing a user interaction?

1 answer

0


Erase

$var = date('H:i:s');
if (strtotime($var) < strtotime('09:34:00') && strtotime($var) > strtotime('15:00:00')) {
    $fp = fopen("log.html", 'w');
    fwrite($fp, "");
    fclose($fp);
}

that strtotime() reduces the time to "int" and after that it checks a time to do the deletion but indicate for a distance time for if there is some delay not cancel your deletion, you can by a cornjob if you want to automate...

Delete /cornjob

    $fp = fopen("log.html", 'w');
    fwrite($fp, "");
    fclose($fp);
  • It works, but you need to re-load the page. But I wish there was no need to make a page request, IE, the user should not give a Reload on the page for the log to be deleted. I would like the log to be deleted without interaction at a time (at 02:00 am).

  • CORNJOB then to make automatic

  • http://blog.thiagobelem.net/o-que-sao-e-como-usar-as-cron-jobs or ce use cpainel search for cornjob Cpanel

  • you can even remove if if it is by cornjob

  • if the problem is solved, check how validates the answer, to stay as solved problem for the next

Browser other questions tagged

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