Вот, я использую 2 файла PHP. Сам чат находится в файле "chat.txt".
chatburn.php
<?php
/* takes the file and puts it in array form */
$chatfile = fopen("chat.txt", "r") or exit("cannot open file");
$chatarray=array();
while(!feof($chatfile))
{
$newline=explode("\n", fgets($chatfile));
unset($newline[1]);
$chatarray[]=$newline;
}
fclose($chatfile);
?>
chat.php
<?php
session_start();
require_once 'chatburn.php';
?>
<form method="post">
<label>message:</label><input type="text" name="message">
<br>
<input type="submit" name="formsubmitted">
</form>
<?php
if (isset($_REQUEST['formsubmitted']))
{
$file = fopen("chat.txt", "a") or exit("cannot open file");
$newdata= "\r\n" . $_SESSION['user']. ": " .$_REQUEST['message'];
fwrite($file, $newdata) or exit("failed to write to file");
fclose($file);
require_once 'chatburn.php';
echo $chatarray[count($chatarray)-5][0];
echo "<br>";
echo $chatarray[count($chatarray)-4][0];
echo "<br>";
echo $chatarray[count($chatarray)-3][0];
echo "<br>";
echo $chatarray[count($chatarray)-2][0];
echo "<br>";
echo $chatarray[count($chatarray)-1][0];
}
?>