= 50) { // Trim the oldest messages, keeping only the latest 49 messages $lines = array_slice($lines, -49); } // Write the new message to the file file_put_contents($filename, implode(PHP_EOL, $lines) . PHP_EOL . $newMessage); // Return a success response http_response_code(200); exit('Message saved successfully.'); } else { // Invalid input, return an error response http_response_code(400); exit('Invalid input data.'); } } else { // Handle GET requests (fetching chat messages) if ($_SERVER['REQUEST_METHOD'] === 'GET') { $filename = 'chatlog.txt'; if (file_exists($filename)) { // Read the chat log file and send its content as the response readfile($filename); } else { // Return an empty response if the chat log file doesn't exist http_response_code(200); exit(''); } } } ?>