首頁  >  文章  >  後端開發  >  無資料庫支援的留言板

無資料庫支援的留言板

WBOY
WBOY原創
2016-07-25 09:02:301980瀏覽
  • 無資料庫支援的簡單留言板,主要是對檔案方式儲存和讀取資料的練習。
    1. /**
    2. * 這是一個單一頁面沒有資料庫支援的留言板系統
    3. * 知識點:
    4. * 1、heredoc文件的使用:>>>EOT EOT; 第二個EOT行前不能有任何空格
    5. * 2、檔案的讀寫操作
    6. * 3、fread和fgets區別,fread讀取指定長度的字串,fgets讀取一行,剛好保存資料的時候一行是一個留言內容,利於讀取
    7. *
    8. * 4、檔案鎖,本版還沒實施,只寫出了參考碼。
    9. *
    10. */
    11. $file = "message.txt";
    12. if(isset($_POST )&&!empty($_POST)){
    13. $post = $_POST;
    14. $content ="標題:".$post['title'].' 內容:'.$post['message'] ."nr";
    15. if( file_exists($file) ){
    16. add_message($file,$content);
    17. }else{
    18. create_message_file($file,$content);
    19. }
    20. }
    21. /**
    22. * 初次使用時建立留言檔案並儲存留言
    23. * Enter description here ...
    24. * @param unknown_type $file
    25. * @param unknown_type $message
    26. */
    27. function create_message_file($file,$message){
    28. $msgh = fopen($file,"w") ;
    29. //flock($file, LOCK_EX);
    30. fwrite($msgh,$message);
    31. fclose($msgh);
    32. //echo "新增留言訊息成功。";
    33. echo
    34. EOT;
    35. }
    36. /**
    37. * 顯示留言內容
    38. * Enter description here ...
    39. * @param unknown_type $file
    40. */
    41. function show_message($file){
    42. $msgh = fopen($file, "r");
    43. //flock($msgh, LOCK_EX);
    44. while($msg = fgets($msgh)) {
    45. echo $msg;
    46. echo "
      ";
    47. }
    48. fclose($msgh);
    49. }
    50. ?> ; 🎜>
    51. 無資料庫支援的簡單留言板
    52. 無資料庫支援的簡單留言板 __留言內容顯示

    53. if(!file_exists($file)||filesize($file )?>
    54. }else{
    55. ?>
    56. }
    57. ?>
    58. 暫時還沒留言
    59. show_message($file);
    60. ?>

    ;請在下面輸入您的留言標題和內容
  • 標題 

  • 複製程式碼

  • 陳述:
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn