[PHP] 纯文本查看 复制代码 <?php
$filename = "message.txt";
if (isset($_POST["submit"])){
$message = $_POST["username"]."||".$_POST["title"]."||".$_POST["content"]."[n]";
write_message($filename, $message);
}
// 将留言写入文件
function write_message($filename, $message){
$file = fopen($filename, "a");
fwrite($file, $message);
fclose($file);
}
// 将留言从文件中读取出来,并解析成单条输出
function read_message($filename){
$message = file_get_contents($filename);
$message = rtrim($message, "[n]");
$array = explode("[n]", $message);
foreach($array as $msg){
list($username, $title, $message) = explode("||", $msg);
echo "用户:{$username}<br/>";
echo "标题:{$title}<br/>";
echo "内容:{$message}<br/>";
echo "<hr>";
}
}
?>
<!--制作一个简单的留言输入控件-->
<form action="" method="POST">
用户:<input type="text" name="username" value=""/><br/>
标题:<input type="text" name="title" value="" /><br/>
内容:<textarea cols="40" rows="4" name="content"></textarea><br/>
<input type="submit" name="submit" value="留言"/>
</form>
<hr>
<?php
// 输出留言
if (file_exists($filename)) read_message($filename);
?>
|