Heim  >  Artikel  >  Backend-Entwicklung  >  无数据库支持的留言板

无数据库支持的留言板

WBOY
WBOYOriginal
2016-07-25 09:02:301987Durchsuche
无数据库支持的简单留言板,主要是对文件方式存储和读取数据的练习。
  1. /**
  2. * 这是一个单页面没有数据库支持的留言板系统
  3. * 知识点:
  4. * 1、heredoc文档的使用:>>>EOT EOT; 第二个EOT行前不能有任何空格
  5. * 2、文件的读写操作
  6. * 3、fread和fgets区别,fread读取指定长度的字符串,fgets读取一行,刚好保存数据的时候一行是一个留言内容,利于读取
  7. *
  8. * 4、文件锁,本版还没实施,只写出了参考代码。
  9. *
  10. */
  11. $file = "message.txt";
  12. if(isset($_POST)&&!empty($_POST)){
  13. $post = $_POST;
  14. $content ="标题:".$post['title'].' 内容:'.$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. * 初次使用时创建留言文件并保存留言
  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 "添加留言信息成功。";
  33. echo
  34. EOT;
  35. }
  36. /**
  37. * 添加新留言信息到文件中
  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 "你的留言已成功保存。";
  48. echo
  49. EOT;
  50. }
  51. /**
  52. * 显示留言内容
  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. ?>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn