<?php $dataFile = 'bbs.dat'; //CSRF対策 session_start(); function setToken() { $token = sha1(uniqid(mt_rand(), true)); $_SESSION['token'] = $token; } function checkToken() { if (empty($_SESSION['token']) || ($_SESSION['token'] != $_POST['token'])) { echo "不正な処理が行われました"; exit; } } function h($s) { return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); } if ($_SERVER['REQUEST_METHOD'] = 'POST' && isset($_POST['message']) && isset($_POST['user'])) { $message = $_POST['message']; $user = $_POST['user']; } if ($message != '') { $user = ($user == '') ? 'ななしさん' : $user; $postedAt = date('Y-m-d H:i:s'); // $postedAt = date('Y-m-d H:i:s'); $message = str_replace( "\t",' ',$message); $user = str_replace( "\t",' ',$user); $newData = $message . "\t" . $user . "\t" . $postedAt. "\n"; $fp = fopen($dataFile, 'a'); fwrite($fp, $newData); fclose($fp); } $posts = file($dataFile, FILE_IGNORE_NEW_LINES); $posts = array_reverse($posts); ?>
<form action="" method="post"> <dl> <dd style="margin-bottom:5px"><B>User名</B></dd><textarea name="user" cols="10" rows="1"></textarea> <dd> <dt>○発言どうぞ</dt> <textarea name="message" cols="50" rows="5"></textarea> </dd> </dl> <div> <input type="submit" value="投稿する" /> <input type="hidden" name="token" value="<?php echo h($_SESSION['token']);?>"> </div> <h3>投稿一覧 (<?php echo count($posts); ?>件) </h3> <ul> <?php if (count($posts)) : ?> <?php foreach ($posts as $post) : ?> <?php list($message, $user, $postedAt) = explode("\t", $post); ?> <li><?php echo h($user); ?> <?php echo h($message); ?> <?php echo h($postedAt); ?></li> <?php endforeach; ?> <?php else : ?> <li>まだ投稿がありません。</li> <?php endif; ?> </ul> </div>
上記コードで、F5でHP更新時、前の投稿が掲示板に、そのまま現れます。
下記URLを参考に
http://www.phppro.jp/qa/262#1689
headerを利用し、この問題を解決したいのですが、
header("Location:end.html"); exit();
をどこのコードに入れれば、宜しいでしょうか。
回答1件
あなたの回答
tips
プレビュー