I want to read in a message from a JS file, and then change a global variable in that js file so that it will adjust its performance (and then empty the text file). Unfortunately, it always returns the original value of the text file instead of the updated value, and I can't use the updated value until I refresh the page. Is there a better way to do this?
<?php
switch($_SERVER['REQUEST_METHOD'])
{
case 'GET':
if(file_exists('chat.txt') && filesize('chat.txt') > 0){
$filesize = filesize('chat.txt');
$f = fopen('chat.txt', "a+");
$line = fread($f, $filesize);
$line = htmlspecialchars(str_replace("
", "", $line));
echo "<script> post = '" . $line . "';</script>";
fclose($f);
fclose(fopen('chat.txt', 'w'));
}
break;
case 'POST':
$function = $_POST['function'];
switch ($function) {
case('send'):
$message = strip_tags($_POST['message']);
$f = fopen('chat.txt', 'w');
fwrite($f, $message);
fclose($f);
break;
}
}
?>