###前提・実現したいこと
サイトのコードを信用していいものか
投稿したときのNo1の下の空欄が出来てしまうのでどうにかしたい。
###発生している問題・エラーメッセージ
掲示板を作るサイトを見つけまして ポンクソフト やってみてたんですがどうやら最初おかしいらしくエラーがでまくります。少しずつ直して後は一番下のprintエラーだけになりました。これがどうも直らないので対処法を教えてください。また最初からおかしいのでこのサイトは信用してはいけないのでしょうか?
画像は最初の場面で
画像の上の部分は全部クリアしました。
残ってるエラーは下のやつだけです。
Undefined offsetなので調べてみた結果配列がありませんとか言うエラーだと思います。
一番最初なのでテキストが空白なのでありませんって言われてエラー文がPRINTされてしまうものだという予想。
###該当のソースコード
<?php $lines = file('bbs2.txt'); if ($_POST['write']) { $items = explode("\t", $lines[0]); $no = $items[0] + 1; $name = htmlspecialchars($_POST['name']); if (!$name) $name = "名無しさん"; $mail = htmlspecialchars($_POST['mail']); $title = htmlspecialchars($_POST['title']); if (!$title) $title = "無題"; $contents = htmlspecialchars($_POST['contents']); $contents = str_replace("\r\n", "<br>", $contents); $contents = str_replace("\r", "<br>", $contents); $contents = str_replace("\n", "<br>", $contents); $delkey = htmlspecialchars($_POST['delkey']); $time = date("Y/m/d H:i:s"); $expire = time() + 3600 * 24 * 30; setcookie("name", $name, $expire); setcookie("mail", $mail, $expire); setcookie("delkey", $delkey, $expire); $data = "$no\t$name\t$mail\t$title\t$contents\t$delkey\t$time\n"; array_unshift($lines, $data); } else { $name = $_COOKIE['name']; $mail = $_COOKIE['mail']; $delkey = $_COOKIE['delkey']; } if ($_POST['delete']) { for ($i = 0; $i < count($lines); $i++) { $items = explode("\t", $lines[$i]); if ($items[0] == $_POST['delno'] && $items[5] == $_POST['delkey2']) { array_splice($lines, $i, 1); } } } if ($_POST['write'] || $_POST['delete']) { $fp = fopen('bbs2.txt', 'w'); foreach($lines as $line) fputs($fp, $line); fclose($fp); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> <title>掲示板2</title> </head> <body> <form method="post" action="bbs2.php"> お名前:<input type="text" name="name" value="<?php print $name ?>"><br> メール:<input type="text" name="mail" value="<?php print $mail ?>"><br> 題 名:<input type="text" name="title"><br> 削除キー:<input type="password" name="delkey" value="<?php print $delkey ?>"><br> <textarea name="contents" cols="60" rows="5"></textarea><br> <input type="submit" name="write" value="送信"> <hr> 記事番号:<input type="text" name="delno"> 削除キー: <input type="password" name="delkey2"> <input type="submit" name="delete" value="記事削除"> </form> <hr> <?php foreach($lines as $line) { $line = rtrim($line); $items = explode("\t", $line); print "No.{$items[0]} "; print "<b>{$items[3]}</b> 投稿者:"; if ($items[2]) print "<a href='mailto:{$items[2]}'>"; print $items[1]; if ($items[2]) print "</a>"; print " 投稿時間:{$items[6]}"; print "<p>{$items[4]}</p><hr>\n"; } ?> </body> </html>
自分がやったのとさほどかわらないのでサイトのまま乗せてます。
自分の修正版一応エラーは消えてる
<?php $lines = file('bbs2.txt'); if(!isset($_POST['write'])){ $name = $_COOKIE['name']; }else{ if($_POST['write']){ $items = explode("\t", $lines[0]); $no = $items[0] + 1; $name = htmlspecialchars($_POST['name']); if (!$name) $name = "名無しさん"; $mail = htmlspecialchars($_POST['mail']); $title = htmlspecialchars($_POST['title']); if (!$title) $title = "無題"; $contents = htmlspecialchars($_POST['contents']); $contents = str_replace("\r\n", "<br>", $contents); $contents = str_replace("\r", "<br>", $contents); $contents = str_replace("\n", "<br>", $contents); $delkey = htmlspecialchars($_POST['delkey']); $time = date("Y/m/d H:i:s"); $expire = time() + 3600 * 24 * 30; setcookie("name", $name, $expire); setcookie("mail", $mail, $expire); setcookie("delkey", $delkey, $expire); $data = "$no\t$name\t$mail\t$title\t$contents\t$delkey\t$time\n"; array_unshift($lines, $data); } } if(isset($_POST['delete'])) { for ($i = 0; $i < count($lines); $i++) { $items = explode("\t", $lines[$i]); if ($items[0] == $_POST['delno'] && $items[5] == $_POST['delkey2']) { array_splice($lines, $i, 1); } } } if(isset($_POST['write'])){ $fp = fopen('bbs2.txt', 'w'); foreach($lines as $line) fputs($fp, $line); fclose($fp); } if(isset($_POST['delete'])){ $fp = fopen('bbs2.txt', 'w'); foreach($lines as $line) fputs($fp, $line); fclose($fp); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> <title>掲示板2</title> </head> <body> <form method="post" action="tes.php"> お名前:<input type="text" name="name" value="<?php print $name ?>"><br> メール:<input type="text" name="mail" value="<?php print $mail ?>"><br> 題 名:<input type="text" name="title"><br> 削除キー:<input type="password" name="delkey" value="<?php print $delkey ?>"><br> <textarea name="contents" cols="60" rows="5"></textarea><br> <input type="submit" name="write" value="送信"> <hr> 記事番号:<input type="text" name="delno"> 削除キー: <input type="password" name="delkey2"> <input type="submit" name="delete" value="記事削除"> </form> <hr> <?php foreach($lines as $line) { $line = rtrim($line); $items = explode("\t", $line); print "No.{$items[0]} "; print "<b>{$items[3]}</b> 投稿者:"; if ($items[2]) print "<a href='mailto:{$items[2]}'>"; print $items[1]; if ($items[2]) print "</a>"; print " 投稿時間:{$items[6]}"; print "<p>{$items[4]}</p><hr>\n"; } ?> </body> </html>
###試したこと
IFをつけてやりたかったのだがいい条件式が探してみたが見つからなかった。
グローバル変数で先に$itemsで配列を作ったがまったく意味なかった。
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答4件
あなたの回答
tips
プレビュー