The example of this paper shares the realization idea of php message board for your reference. The specific content is as follows:
1. Create a file name to store message information
2. Get the data in the form to a variable
3. Exist when judging documents
4. Write to the file. Before you open the file, choose the access method and close the file
5. Read the file and close the file
1 <?php 2 3 //Idea of message board: 1.Create a file name first, which is convenient for storing the written content 4 // 2. Assign the contents of the form to a variable 5 //3. Judge whether the file exists, write the value entered by the user into the variable, and pay attention to the operation of file access when opening the file 6 //4. Read the contents of the file and close the file 7 8 9 header("Content-Type:text/html;charset=utf8"); 10 $filename = "message.txt";//Create a file name 11 12 //If the user submits it, write it to a file in a certain format 13 if(isset($_POST['dosubmit'])) { 14 //Separate use of fields||, Separate use of rows[n] 15 $mess = "{$_POST['username']}||".time()."||{$_POST['title']}||{$_POST['content']}[n]"; 16 17 18 writemessage($filename, $mess);//Write content to file 19 20 } 21 22 if(file_exists($filename)) {//Judge whether the file exists 23 readmessage($filename);//Functions to read files 24 } 25 26 27 function writemessage($filename, $mess) { 28 $fp = fopen($filename, "a");//Write at the end without deleting the original file content 29 fwrite($fp, $mess);//write file 30 31 fclose($fp);//Close file 32 } 33 34 function readmessage($filename) { 35 $mess = file_get_contents($filename); 36 $mess = rtrim($mess, "[n]"); 37 38 $arrmess = explode("[n]", $mess); 39 40 foreach($arrmess as $m) { 41 list($username, $dt ,$title, $content) = explode("||", $m); 42 43 echo "{$username}, ".date("Y-m-d H:i").": <i>{$title}</i>, <u>{$content}</u><br><hr><br>"; 44 } 45 46 } 47 48 ?> 49 50 <form action="message.php" method="post"> 51 user: <input type="text" name="username" value="" /><br> 52 Title:<input type="text" name="title" value="" /><br> 53 Content:<textarea name="content" cols="40" rows="4"></textarea><br> 54 <input type="submit" name="dosubmit" value="Leaving a message." /><br> 55 </form>
The above is the whole content of this article. I hope it will help you in your study
Here are more comments https://www.sucaihuo.com/php/221-0-0-0
Interested friends can have a look