Home  >  Article  >  Backend Development  >  Message board without database support

Message board without database support

WBOY
WBOYOriginal
2016-07-25 09:02:301980browse
A simple message board without database support, mainly an exercise in storing and reading data in files.
  1. /**
  2. * This is a single-page message board system without database support
  3. * Knowledge points:
  4. * 1. Use of heredoc documents: >>>EOT EOT; There cannot be any space before the second EOT line
  5. * 2 , file read and write operations
  6. * 3. The difference between fread and fgets, fread reads a string of specified length, fgets reads a line, when the data is saved, one line is a message content, which is convenient for reading
  7. *
  8. * 4. File Lock, this version has not been implemented yet, only the reference code is written.
  9. *
  10. */
  11. $file = "message.txt";
  12. if(isset($_POST)&&!empty($_POST)){
  13. $post = $ _POST;
  14. $content ="title:".$post['title'].' content:'.$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. * Create a message file and save the message when using it for the first time
  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 "Add message message successfully.";
  33. echo <<
  34. EOT;
  35. }
  36. /**
  37. * Add new message information to the file
  38. * Enter description here...
  39. * @param unknown_type $file
  40. * @param unknown_type $message
  41. */
  42. function add_message($file,$message){
  43. $msgh = fopen($file, "a");
  44. //flock($msgh,LOCK_EX) ;
  45. fwrite($msgh,$message);
  46. fclose($msgh);
  47. //echo "Your message has been saved successfully.";
  48. echo <<
  49. EOT;
  50. }
  51. /**
  52. * Display message content
  53. * Enter description here ...
  54. * @param unknown_type $file
  55. */
  56. function show_message($file){
  57. $msgh = fopen($file, "r");
  58. //flock($msgh, LOCK_EX);
  59. while($msg = fgets($msgh)){
  60. echo $msg;
  61. echo "
    ";
  62. }
  63. fclose($msgh);
  64. }
  65. ?>
  66. 无数据库支持的简单留言板
  67. 无数据库支持的简单留言板__留言内容显示

  68. if(!file_exists($file)||filesize($file)<1){
  69. ?>
  70. }else{
  71. ?>
  72. }
  73. ?>
  74. 暂时还没有留言
  75. show_message($file);
  76. ?>

  77. 请在下面输入你的留言标题和内容
  • 复制代码


    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn