To insert into rows instead of using file_put_contents
use fwrite
(or fput
) and it will be necessary to use PHP_EOL
for line breaks.
Note it is recommended to convert the message into html entities to avoid attacks XSS for example, for this use htmlspecialchars
.
An example for recording would be:
<?php
$nome = htmlspecialchars($_POST['nome']);
$mensagem = htmlspecialchars($_POST['mensagem']);
$linha = $nome . ' - ' . $mensagem . '<br>' . PHP_EOL;
$handle = fopen('chatlog.html', 'a'); //O 'a' põe o ponteiro no final, assim a grava no final do arquivo
fwrite($handle, $linha);
fclose($handle);
I noticed you’re recording everything into one .html
this means that maybe the reading is done directly in HTML, maybe an iframe or ajax that keeps doing Reload chatlog.html
, this works, but the bigger the file the more time-consuming the page reply.
Note: the following steps are optional and do not have to do with the problem, consider as a tip only, being totally optional, this process described is preferred to work with Ajax or something similar and DOM manipulation by javascript.
A way the file can be using fopen
, feof
and fgets
(fgets
reads line to line different from fread
which reads by size), will be required $_SESSION
or normal cookies.
The php reading file should be something like (from a name like chatreader.php):
<?php
session_start();
//Verifica a última linha que o usuário leu
if (empty($_SESSION['currentLine'])) {
//Se é a primeira vez que o usuário acessa então a leitura começa do '0'.
$_SESSION['currentLine'] = 0;
}
$current = $_SESSION['currentLine'];
$i = 0;
$handle = fopen('chatlog.html', 'r');
while (false === foef($handle)) {
++$i; //Soma para contar as linhas
//Se $i for maior que $current então significa que a linha não foi lida
if ($i > $current) {
echo fgets($handle);
}
}
$_SESSION['currentLine'] = $i; //Grava a ultima posição de leitura
Javascript would look something like:
<div id="conversa"></div>
<script type="text/javascript">
function xhr() {
var xmlhttp = false;
if (XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else if(ActiveXObject) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch(ee){}
}
}
return xmlhttp;
}
function chatReader(target) {
var x = xhr();
if (x === false) {
return;
}
x.open("GET", "chatreader.php?_=" + (new Date().getTime()), true);
x.onreadystatechange = function()
{
if (x.readyState === 4){
if (x.status === 200) {
var nd = document.createElement("div");
nd.innerHTML = x.responseText;
target.appendChild(nd);
} else {
console.log("Erro no servidor", x.status);
}
setTimeout(function() {
chatReader(target);
}, 1000);
}
};
x.send(null);
}
window.onload = function() {
var conversa = document.getElementById("conversa");
if (conversa) {
chatReader(conversa);
}
};
</script>
Why not save as txt, since you treat as text file?
– user28595
Another thing, add one
\n
right next to the<br>
, so I think it’s enough to save line by line.– user28595
I changed to txt but the n is leaving in the imprint of the code
– Arsom Nolasco
Change to
"<br>\n"
. Recalling that the<br>
is html tag, unless you open the file in the browser, it will be ignored.– user28595
continues to write n on print and txt is also not bouncing line in txt file
– Arsom Nolasco
Well I created this mini tutorial many years ago, it talks how to transform Database with TXT Maybe it’ll help you do what you want.
– SK15
Would saving with PHP_EOL help? PHP_EOL skips a line while writing to the file.
– Marcos Xavier