데이터베이스를 지원하지 않는 간단한 게시판으로, 주로 파일에 데이터를 저장하고 읽는 연습을 합니다.
- /**
- * 데이터베이스 지원이 없는 단일 페이지 게시판 시스템입니다
- * 지식 포인트:
- * 1. heredoc 문서 사용: >>>EOT EOT 두 번째 줄은 앞에 올 수 없습니다. 공백이 있습니다
- * 2. 파일 읽기 및 쓰기 작업
- * 3. fread와 fgets의 차이점 fread는 지정된 길이의 문자열을 읽고, fgets는 데이터를 저장할 때 한 줄을 읽습니다. 읽기 편한 메시지 내용입니다.
- *
- * 4. 이번 버전에서는 파일 잠금이 구현되지 않았고, 참고 코드만 작성되었습니다.
- *
- */
-
- $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);
- }
- }
-
-
- /**
- * 메시지 파일을 생성하고 처음 사용할 때 메시지를 저장하세요
- * 여기에 설명을 입력하세요...
- * @paramknown_type $file
- * @paramknown_type $message
- */
- function create_message_file($file,$message){
- $msgh = fopen($file,"w" );
- //flock($file, LOCK_EX);
- fwrite($msgh,$message);
- fclose($msgh);
- //echo "메시지 메시지를 성공적으로 추가했습니다.";
- echo <<
- Alert("메시지 메시지를 성공적으로 추가했습니다.");
- top.location='message.php' ;
-
- EOT;
-
- }
-
- /**
- * 파일에 새 메시지 정보 추가
- * 여기에 설명을 입력하세요...
- * @paramknown_type $file
- * @paramknown_type $message
- */
- 함수 add_message($file,$message){
- $msgh = fopen($file, "a");
- //flock($msgh,LOCK_EX)
- fwrite($msgh,$message);
- fclose($msgh);
- //echo "메시지가 성공적으로 저장되었습니다.";
- echo <<
- Alert("메시지가 성공적으로 저장되었습니다. .");
- top.location='message.php';
-
- EOT;
- }
-
- /**
- * 메시지 내용 표시
- * 여기에 설명을 입력하세요...
- * @paramknown_type $file
- */
- function show_message($file){
- $msgh = fopen($file, "r");
- //flock($msgh, LOCK_EX);
- while($msg = fgets($msgh)) {
- echo $msg;
- echo "
";
- }
- fclose($msgh);
- }
-
-
-
- ?> ;
-
-
- 无数据库支持的简单留言板
- body{
- 여백 :0px;
- 패딩:0px;
- }
- #message{
- 너비:960px;
- 여백:0px auto;
- 패딩:10px;
- 오버플로:hidden;
- 배경:#CCCCCC;
- }
-
- <시간>시간>
-
- if(!file_exists($file)||filesize($file )<1){
- ?>
-
-
暂时还没有留言 |
-
- }else{
- ?>
-
-
- show_message($file);
- ?>
-
|
-
- }
- ?>
-
;/hr>
제제대码
|