Message boards with no database support

Source: Internet
Author: User
Tags flock fread
A simple message board with no database support, mainly the practice of storing and reading data in a file way.
  1. /**
  2. * This is a single page without database support of the message board system
  3. * Knowledge Points:
  4. * 1, the use of Heredoc documents:>>>eoteot; Cannot have any spaces before the second EOT line
  5. * 2, the file read and write operation
  6. * 3, Fread and fgets differences, fread read the specified length of the string, fgets read a line, just save the data when a line is a message content, conducive to read
  7. *
  8. * 4, File lock, this version has not been implemented, only wrote the reference code.
  9. *
  10. */
  11. $file = "Message.txt";
  12. if (Isset ($_post) &&!empty ($_post)) {
  13. $post = $_post;
  14. $content = "title:". $post [' title ']. ' content: '. $post [' message ']. " \n\r ";
  15. if (file_exists ($file)) {
  16. Add_message ($file, $content);
  17. }else{
  18. Create_message_file ($file, $content);
  19. }
  20. }
  21. /**
  22. * Create message file and save message when first use
  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 "added message message successfully. ";
  33. Echo <<<>
  34. EOT;
  35. }
  36. /**
  37. * Add new message information to 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 successfully saved. ";
  48. Echo <<<>
  49. EOT;
  50. }
  51. /**
  52. * Show 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. Simple message board with no database support
  67. Simple message Board without database support _ message content display
  68. if (!file_exists ($file) | | FileSize ($file) <1) {
  69. }else{
  70. }
  71. ?>
  72. ?>
  73. ?>
  74. No messages yet.
  75. Show_message ($file);
  76. ?>
  77. Please enter your message title and content below.
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.