ネタで・・・
掲示板の回答ソースを晒してみる。
夜中マックで飯食いながら考えた適当な実装なのであしからず。
- データはtsv形式でファイルに保存する
- データのチェックはザル
list.php
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=euc-jp"> <title>簡易掲示板</title> </head> <body> <font color="green" size="6">けいじばん</font> <p></p> <form method="post" action="write.php"> 名前:<input type="text" name="user"/><br> 発言:<textarea name="contents" cols="50" rows="3" wrap="hard"></textarea><br> <input type="submit" value="発言する"/> </form> <hr> <div style='margin-left: 15px;'> <?php require_once('logic.php'); make_list_html(); ?> </div> </body> </html>
write.pp
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=euc-jp"> <title>簡易掲示板-書き込み-</title> </head> <body> <?php require_once('logic.php'); write_entity($_POST); ?> <p></p> <a href="list.php">一覧に戻る</a> </body> </html>
logic.php
<?php function write_entity($post){ if(check($post)){ $write_data = write_convert($post); write_file($write_data); print("書き込み処理が正常に終了しました。"); } else { print("入力に不備があります。"); } } function check($post){ $user = $post['user']; $contents = $post['contents']; if(empty($user) || empty($contents)){ return FALSE; } return TRUE; } function write_convert($post){ $user = $post['user']; $contents = $post['contents']; $user = trim($user); $contents = preg_replace('/(\r)*\n/', '<br>', $contents); $contents = preg_replace('/\t/', ' ', $contents); $date = strftime('%Y/%m/%d %H:%M:%S',time()); $data = join("\t",array($date, $user, $contents, "\n")); return $data; } function write_file($data){ $wfp = fopen('board.dat', 'ab'); flock($wfp, LOCK_EX); fwrite($wfp, $data); flock($wfp, LOCK_UN); fclose($wfp); } function read_file(){ return file('board.dat'); } function read_convert($line){ $entity = split("\t",rtrim($line)); $user = $entity[1]; $date = $entity[0]; $contents = $entity[2]; printf('<font color="blue" size="5">%s</font> %s<br><p></p>%s<p></p>', $user, $date, $contents); print("<hr>\n"); } function make_list_html(){ $data = array_reverse(read_file()); if(count($data) == 0 || empty($data)){ print('<font color="red" size="4">現在書き込みはありません。</font>'); } else { foreach($data as $line){ read_convert($line); } } } ?>