PHP Simple Message Board

Source: Internet
Author: User
Tags flock

/* *  file locking mechanism  * flock () lightweight consultation file lock  * lock_sh get a shared lock (read program).   * LOCK_EX  obtains an exclusive lock (written program.   * LOCK_UN  release Lock (whether shared or exclusive).   *  If you do not want  flock ()   to block when locked, it is &NBSP;LOCK_NB (not supported on windows ).   *   */ /*  *  Message Board   */     $filename  =  "Message.txt";     if (Isset ($_post[' dosubmit ')) {     Separation of     //Fields | |    Delimited [n]         $mess  =  "{$_post[' username ']}| |". Time (). "| | {$_post[' title ']}| | {$_post[' content '} [n] ";         writemessage ($filename,  $mess);     }    if (File_exists ($filename)) {         Readmessage ($filename);     }    //reading Information     function  readMessage ($filename) {          //        $mess  = file_get_contents ($filename);                  $mess  =  "";                  $fp  = fopen ($filename,  "R");                 flock ($FP,  LOCK _SH+LOCK_NB);//Read lock                  while (!feof ($fp)) {                      $mess  .=fread ($fp,  1024);                 }              &nBsp;  flock ($fp, &NBSP;LOCK_UN+LOCK_NB);//Release lock                   $mess  = rtrim ($mess, "[n]");                  $arrmess  = explode ("[n]",  $ Mess);                 foreach ( $arrmess  as  $m) {                     list ($username, $daytime, $title, $content)  = explode ("| |",  $m );                     echo  "<b>{$username}</b>,". Date (' Y-m-d h:i:s ', $daytime). ", <i>{$title} </i><br/><u>{$content}</u><br/><br/><br/> ";                 }                 fclose ($fp);     }    //Write a message     function writemessage ($filename, $mess) {          $FP  = fopen ($filename,  "a")        //lock file          if (Flock ($FP, &NBSP;LOCK_EX+LOCK_NB)) {             fwrite ($fp,  $mess);             flock ($fp, LOCK_UN+LOCK_NB);//Release file         }else {            echo  ' Write lock failed! ';         }        fclose ($FP);     }?><form action= "index.php"  method= "POST" > User name: <input type= "text"  name= "username"   Value= ""/><br/> title: <input type= "Text"  name= "title"  value= "/><br/> content:< Textarea name= "Content"  cols= " rows=" 4 "></textarea><br/><input type=" Submit " name=" Dosubmit " value=" message "/></form>


This article is from the "Jin Sha Harbor" blog, please be sure to keep this source http://11410485.blog.51cto.com/11400485/1841513

PHP Simple Message Board

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.