php+txt写的一个简单的留言板程序

        <p>
在PHP中,可以使用文本文件来实现简单的留言板功能。下面是一段示例PHP代码,可用于实现此功能:

<?php

// Set the name of the text file that will store the messages $filename = "messages.txt";

// Check if the form was submitted if (isset($_POST['submit'])) { // Read the name and message from the form $name = $_POST['name']; $message = $_POST['message'];

// Append the name and message to the messages file file_put_contents($filename, $name . ": " . $message . "\n", FILE_APPEND); }

// Read the messages from the file and store them in an array $messages = file($filename, FILE_IGNORE_NEW_LINES);

// Loop through the messages array and display each message foreach ($messages as $message) { echo $message . "<br>"; }

?> <form action="index.php" method="post"> <input type="text" name="name" placeholder="Your name"> <input type="text" name="message" placeholder="Your message"> <input type="submit" name="submit" value="Submit"> </form>

该代码使用PHP的file_put_contents()和file()函数来读写文本文件。当用户提交表单时,代码会读取用户的姓名和留言,并将其附加到留言文件中。之后,它会读取留言文件中的所有留言,并将它们显示在网页上。


该示例代码简单易懂,可用于实现简单的留言板功能。但是,它并不安全,不能用于生产环境中。例如,如果有人将留言文件上传到网站上,则可以读取其中的所有留言。因此,在实际应用中,需要考虑安全性问题,并采取相应的措施