A simple message board without database support, mainly an exercise in storing and reading data in files.
- /**
- * This is a single-page message board system without database support
- * Knowledge points:
- * 1. Use of heredoc documents: >>>EOT EOT; There cannot be any space before the second EOT line
- * 2 , file read and write operations
- * 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
- *
- * 4. File Lock, this version has not been implemented yet, only the reference code is written.
- *
- */
-
- $file = "message.txt";
- if(isset($_POST)&&!empty($_POST)){
- $post = $ _POST;
- $content ="title:".$post['title'].' content:'.$post['message']."nr";
- if( file_exists($file) ){
- add_message($ file,$content);
- }else{
- create_message_file($file,$content);
- }
- }
-
-
- /**
- * Create a message file and save the message when using it for the first time
- * 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 "Add message message successfully.";
- echo <<
- alert("Add message successfully.");
- top.location='message.php';
-
- EOT;
-
- }
-
- /**
- * Add new message information to the file
- * 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 "Your message has been saved successfully.";
- echo <<
- alert("Your message has been saved successfully.");
- top.location='message.php';
-
- EOT;
- }
-
- /**
- * Display message content
- * 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);
- }
-
-
-
- ?>
-
-
- 无数据库支持的简单留言板
- body{
- margin:0px;
- padding:0px;
- }
- #message{
- width:960px;
- margin:0px auto;
- padding:10px;
- overflow:hidden;
- background:#CCCCCC;
- }
-
- if(!file_exists($file)||filesize($file)<1){
- ?>
-
-
暂时还没有留言 |
-
- }else{
- ?>
-
-
- show_message($file);
- ?>
-
|
-
- }
- ?>
-
复制代码
|