I'm doing a chat using PHP, JavaScript, Ajax and HTML. I know this is not the best option, but I chose to create a log.html
html file that stores all messages and is uploaded by my index.php
PHP file. All messages are sent to the log via the post.php
file. I need to delete all messages, ie delete all the content of the html present in log.html
at a certain time. How can I do this? Follow the code for the files.
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 do I delete all contents of log.html
at a particular time without needing a user interaction?