2
2

More than 1 year has passed since last update.

Web掲示板を作成しました(PHP)

Posted at

PHPでWeb掲示板を作成しました。
実装した機能は
1.NGワードは投稿できない
2.スペースのみ、0文字はNG
です。

投稿フォーム(index.php)

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>BBS掲示板</title>
    <link rel="stylesheet" href="common.css">
</head>
<body>

</body>
    <form method="post" action="entry.php">
        <label>タイトル</label>
        <br>
        <input type="text" name="title" >
        <br>
        <label>書き込み内容</label>
        <br>
        <textarea id="sum" name="sum" rows="3" cols="35">
        </textarea>
        <br>
        <input type="submit" value="送信">
        <br>
    </form>

    <p class="ichiran"> 投稿一覧 </p>
    <?php
        $fp = file_get_contents("bbs.txt");
        echo $fp;
    ?>
</html>

ファイル書き込み処理(entry.php)

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>BBS掲示板</title>
    <link rel="stylesheet" href="common.css">
</head>
<body>
    <?php
        // メッセージ出力変数の定義
        $msg1 = "タイトルを入力してください";
        $msg2 = "内容を入力してください";
        // メッセージ出力配列の定義
        $msg_arr = [];
        // NGワードリスト配列の定義
        $list = ["死ね","まんこ","ちんこ","自殺しろ","ガイジ","障害","カタワ"];

        // リンク先表示関数
        function out_url() {
            echo "<a href=index.php>掲示板へ</a>";
        }

        // 文字列の長さ取得関数
        function count_str($str){
            $str = str_replace(array(" "," "),"",$str);
            return strlen($str);
        }

        // NGワードチェック処理
        function ng_word($str){
            global $list;
            $ng_count = 0;
            foreach($list as $word){
                $w_str = "/" . $word . "/";
                $a = preg_match($w_str,$str);
                if ($a == true){
                  $ng_count += 1;
                }
            }
            return $ng_count;

        }


        // タイトルを取得する変数
        $title = $_POST["title"];
        #入力チェック関数
        $count1 = count_str($title);
        // 入力チェック確認
        if($count1 == 0){
            array_push($msg_arr,$msg1);
        }
        // 内容を取得する変数
        $sum = $_POST["sum"];
        // 入力チェック関数
        $count2 = count_str($sum);
        // 入力チェック確認
        if($count2 == 0){
            array_push($msg_arr,$msg2);
        }
        $num = count($msg_arr);
        #入力されていない項目があれば表示して処理を終了する
        if($num >= 1){
            foreach($msg_arr as $msg){
                echo "<p class='errmsg'>{$msg}</p>";
            }
            out_url();
            exit();
        }

        $title_check = ng_word($title);
        if ($title_check >= 1){
            echo "<p class='ng1'>タイトルにNGワードが含まれています</p>";
            out_url();
            exit();
        }

        $sum_check = ng_word($sum);
        if ($sum_check >= 1){
            echo "<p class='ng1'>内容にNGワードが含まれています</p>";
            out_url();
            exit();
        }

        // 日付を取得
        $date1 = date("Y年m月d日 H時i分s秒");
        // ミリ秒を取得
        $w_time = microtime(true);
        $w_array = explode('.',$w_time);
        $mtime = $w_array[1];
        $microtime = intval($mtime / 100);
        if($Microtime < 10){
            $microtime = "0" . $microtime;
        }

        // ファイルに書き込む内容を変数に代入する。
        $result = "<br>". "日付:" .$date1 . $microtime . "<br>" . "タイトル:" . $title . "<br>" . "内容:" . $sum;

        // 書き込みモードでファイルを開く
        $write1 = file_put_contents("bbs.txt", $result,FILE_APPEND);

        // ファイルの書き込みチェック処理
        if($write1){
            echo "<p class='fmsg'>ファイルの書き込みが成功しました。</p>";
        } else {
            echo "<p class='fmsg'>ファイルの書き込みに失敗しました。</p>";
        }

        out_url();

    ?>
</body>

</html>

CSSファイル(common.css)

@charset 'utf8';

/* エラーメッセージのCSS */
.errmsg {
  font-size:20px;
  color:#D8A125;
  font-weight:bold;
}

/* NGワードのCSS */
.ng1 {
  font-size:20px;
  color:#F200F6;
  font-weight:bold;
}

/* 投稿一覧タグのCSS */
.ichiran {
  color:#c10763;
  font-size:20px;
}

/* ファイルの書き込み成功、失敗メッセージの定義 */
.fmsg {
  color:#0023F7;
  font-size:20px;
  font-weight:bold;
}

ファイル処理を学習した後に実装してみてください。

2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2