無資料庫支援的簡單留言板,主要是對檔案方式儲存和讀取資料的練習。
-
/**
- * 這是一個單一頁面沒有資料庫支援的留言板系統
- * 知識點:
- * 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'] ."nr";
- 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;
- }
-
- /**
- * 顯示留言內容
- * 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 "
";
- }
- fclose($msgh);
- }
-
-
-
- ?> ; 🎜>
-
-
-
-
- 無資料庫支援的簡單留言板
-
-
-
-
-
-
if(!file_exists($file)||filesize($file )?>
-
-
暫時還沒留言 |
-
-
}else{
- ?>
-
-
- show_message($file);
- ?>
-
|
-
-
}
- ?>
-
-
-
-
;請在下面輸入您的留言標題和內容 |
-
|