現在学習で簡易掲示板を作成しております
入力フォームから入力されたものを、ファイルに保存して(bbs.dat)、それを掲示板に書き出しています。
<?php
$dataFile ='bbs.dat';
function h($s){
return hmtlspecialchars($s,ENT_QUOTES,'UTF-8');
}
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['message'])&&
isset($_POST['user']))
{
$message =trim($_POST['message']);
$user =trim($_POST['user']);
if($message !== ''){
$user = ($user === '') ? 'ななしさん' : $user;
$postedAt = date('Y-m-d H:i:s');
$message = str_replace("\t", '', $message);
$user = str_replace("\t", '', $user);
$newData =$message . "\t" . $user . "\t" . $postedAt . "\r\n";
$fp = fopen($dataFile, 'a');
fwrite($fp, $newData);
fclose($fp);
}
}
$posts = file($dataFile, FILE_IGNORE_NEW_LINES);
$posts = array_reverse($posts);
?>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>簡易掲示板</title>
</head>
<boby>
<h1>簡易掲示板</h1>
<form action="" method="POST" >
message: <input type="text" name="message">
user:<input type="text" name="user">
<input type="submit" value="投稿">
</form>
<h2>投稿一覧(<?php echo count($posts);?>件)</h2>
<ul>
<?php if(count($posts)) : ?>
<?php foreach ($posts as $post) : ?>
<?php list($message, $user, $postedAt) = explode("\t",$post); ?>
<li><?php echo h($message); ?>
(<?php echo h($user);?>)-
<?php echo h($postedAt);?></li>
<?php endforeach; ?>
<?php else : ?>
<li>まだ投稿はありません。</li>
<?php endif; ?>
</ul>
</boby>
</html>
実行するとこのようなエラーがでます。
配列のエラーみたいなのですが、原因が分かりませんでした。
アドバイス頂けると助かります
Notice: Undefined offset: 1 in C:\xampp\htdocs\index.php on line 53
Notice: Undefined offset: 2 in C:\xampp\htdocs\index.php on line 53
Fatal error: Uncaught Error: Call to undefined function hmtlspecialchars() in C:\xampp\htdocs\index.php:6 Stack trace: #0 C:\xampp\htdocs\index.php(54): h('\xEF\xBB\xBF') #1 {main} thrown in C:\xampp\htdocs\index.php on line 6