Example code of message board function implemented in PHP

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

    //Idea of message board: 1. Create a file name first, which is convenient for storing the written content
    // 2. Assign the contents of the form to a variable
    //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
    //4. Read the contents of the file and close the file
    $filename = "message.txt";//Create a file name
    //If the user submits it, write it to a file in a certain format
    if(isset($_POST['dosubmit'])) {
    //Fields are delimited with |, rows are delimited with [n]
    $mess = "{$_POST['username']}||".time()."||{$_POST['title']}||{$_POST['content']}[n]";
    writemessage($filename, $mess);//Write content to file
    if(file_exists($filename)) {//Judge whether the file exists
    readmessage($filename);//Functions to read files
    function writemessage($filename, $mess) {
    $fp = fopen($filename, "a");//Write at the end without deleting the original file content
    fwrite($fp, $mess);//write file
    fclose($fp);//Close file
    function readmessage($filename) {
    $mess = file_get_contents($filename);
    $mess = rtrim($mess, "[n]");
    $arrmess = explode("[n]", $mess);
    foreach($arrmess as $m) {
    list($username, $dt ,$title, $content) = explode("||", $m);
    echo "{$username}, ".date("Y-m-d H:i").": <i>{$title}</i>, <u>{$content}</u><br><hr><br>";
    <form action="message.php" method="post">
    //User: < input type = "text" name = "username" value = "" / > < br >
    //Title: < input type = "text" name = "title" value = "" / > < br >
    //Content: < textarea name = "content" cols = "40" rows = "4" > < textarea > < br >
    <input type="submit" name="dosubmit" value="Leaving a message." /><br>

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

Tags: Programming PHP

Posted on Tue, 05 Nov 2019 13:42:26 -0500 by kalinkap