php 无数据库支持的留言板
无数据库支持的留言板 [代码片段(154行)]
<?php /** * 这是一个单页面没有数据库支持的留言板系统 * 知识点: * 1、heredoc文档的使用:>>>EOT EOT; 第二个EOT行前不能有任何空格 * 2、文件的读写操作 * 3、fread和fgets区别,fread读取指定长度的字符串,fgets读取一行,刚好保存数据的时候一行是一个留言内容,利于读取 * * 4、文件锁,本版还没实施,只写出了参考代码。 * */ $file = "message.txt"; if(isset($_POST)&&!empty($_POST)){ $post = $_POST; $content ="标题:".$post['title'].' 内容:'.$post['message']."\\n\\r"; if( file_exists($file) ){ add_message($file,$content); }else{ create_message_file($file,$content); } } /** * 初次使用时创建留言文件并保存留言 * Enter description here ... * @param unknown_type $file * @param unknown_type $message */ function create_message_file($file,$message){ $msgh = fopen($file,"w"); //flock($file, LOCK_EX); fwrite($msgh,$message); fclose($msgh); //echo "添加留言信息成功。"; echo <<<EOT <script language="javascript"> alert("添加留言信息成功。"); top.location='message.php'; </script> EOT; } /** * 添加新留言信息到文件中 * Enter description here ... * @param unknown_type $file * @param unknown_type $message */ function add_message($file,$message){ $msgh = fopen($file, "a"); //flock($msgh,LOCK_EX); fwrite($msgh,$message); fclose($msgh); //echo "你的留言已成功保存。"; echo <<<EOT <script language="javascript"> alert("你的留言已成功保存。"); top.location='message.php'; </script> EOT; } /** * 显示留言内容 * Enter description here ... * @param unknown_type $file */ function show_message($file){ $msgh = fopen($file, "r"); //flock($msgh, LOCK_EX); while($msg = fgets($msgh)){ echo $msg; echo "<br>"; } fclose($msgh); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无数据库支持的简单留言板</title> <style type="text/css"> body{ margin:0px; padding:0px; } #message{ width:960px; margin:0px auto; padding:10px; overflow:hidden; background:#CCCCCC; } </style> </head> <body> <div id="message"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><b>无数据库支持的简单留言板</b>__留言内容显示</td> </tr> </table> <hr></hr> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <?php if(!file_exists($file)||filesize($file)<1){ ?> <tr> <td>暂时还没有留言</td> </tr> <?php }else{ ?> <tr> <td> <?php show_message($file); ?> </td> </tr> <?php } ?> </table> <hr></hr> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><b>请在下面输入你的留言标题和内容</b></td> </tr> <tr> <td> <form name="form1" method="post" action=""> <label>标题 <input type="text" name="title"><br><br></label> <label>内容 <textarea name="message" cols="50" rows="10"></textarea><br><br></label> <label> <input type="submit" name="Submit" value="提交"></label></p> </form> </td> </tr> </table> </div> </body> </html> //该片段来自于http://outofmemory.cn
精彩图集
精彩文章