anonymous No title
No License PHP
2021年05月26日
Copy Clone
<?php
echo "パスワード、編集番号、削除番号は全て半角で入力してください";
$text_file = "kadai_2_6.txt";
$count_file = "kadai_2_6count.txt";
$date = date("Y-m-d");
$edit_form_number = "";
$edit_form_name = "";
$edit_form_comment = "";
$edit_form_password = "";
if (!empty($_POST["submit_edit"])) {
  $text_file_array_edit = file($text_file);
  $edit_number = $_POST["edit_number"];
  $edit_password = $_POST["edit_password"];
  foreach ($text_file_array_edit as $edit_value) {
    $edit_value_explode = explode("<>",$edit_value);
    if ($edit_value_explode[0] == $edit_number && $edit_value_explode[1] == $edit_password) {
      $edit_form_number = $edit_value_explode[0];
      $edit_form_name = $edit_value_explode[2];
      $edit_form_comment = $edit_value_explode[3];
      $edit_form_password = $edit_value_explode[1];
    }
  }
}
if (!empty($_POST["submit_normal"])) {
  $normal_name = $_POST["normal_name"];
  $normal_comment = $_POST["normal_comment"];
  $normal_password = $_POST["normal_password"];
  $fp = fopen($count_file,"r");
  $count = fgets($fp);
  fclose($fp);
  $count+=1;
  $fp = fopen($count_file,"w");
  fwrite($fp,$count);
  fclose($fp);
  $text_contents_normal = $count . "<>" . $normal_password . "<>" . $normal_name . "<>" . $normal_comment . "<>" . $date . "\n";
  $fp = fopen($text_file,"a");
  fwrite($fp,$text_contents_normal);
  fclose($fp);
  if (!empty($_POST["edit_hidden"])) {
    $text_file_array_edit_contents = file($text_file);
    $edit_hidden = $_POST["edit_hidden"];
    foreach ($text_file_array_edit_contents as &$text_contents_edit) {
      $text_contents_edit_explode = explode("<>",$text_contents_edit);
      if ($text_contents_edite_xplode[0] == $edit_hidden) {
        $text_contents_edit = $edit_hidden . "<>" . $normal_password . "<>" . $normal_name . "<>" . $normal_comment . "<>" . $date . "\n";
      }
    }
    $fp = fopen($text_file,"w");
    fwrite($fp,$text_file_array_edit_contents);
    fclose($fp);
  }
}
if (!empty($_POST["submit_delete"])) {
  $text_file_array_delete = file($text_file);
  $delete_number = $_POST["delete_number"];
  $delete_password = $_POST["delete_password"];
  $text_contents_delete = [];
  foreach ($text_file_array_delete as $delete_value) {
    $delete_value_explode = explode("<>",$delete_value);
    if ($delete_value_explode[0] == $delete_number && $delete_value_explode[1] == $delete_password) {
      continue;
    }else{
      $text_contents_delete[] = $delete_value;
    }
  }
  $fp = fopen($text_file,"w");
  fwrite($fp,implode("",$text_contents_delete));
  fclose($fp);
}
 ?>
 <!DOCTYPE html>
 <html lang="ja">
   <head>
     <meta charset="utf-8">
     <title>簡易掲示板</title>
   </head>
   <body>
     <h1>簡易掲示板</h1>
     <form action="kadai_2_6.php" method="post">
       <input type="hidden" name="edit_hidden" value="<?php echo $edit_form_number; ?>">
       名前:<input type="text" name="normal_name" value="<?php echo $edit_form_name; ?>"><br>
       コメント:<input type="text" name="normal_comment" value="<?php echo $edit_form_comment; ?>"><br>
       パスワード:<input type="text" name="normal_password" value="<?php echo $edit_form_password; ?>"><br>
       <input type="submit" name="submit_normal" value="投稿">
    </form>
    <h1>削除フォーム</h1>
    <form action="kadai_2_6.php" method="post">
      削除番号:<input type="text" name="delete_number"><br>
      パスワード:<input type="text" name="delete_password"><br>
      <input type="submit" name="submit_delete" value="削除する">
    </form>
    <h1>編集フォーム</h1>
    <form action="kadai_2_6.php" method="post">
      編集番号:<input type="text" name="edit_number"><br>
      パスワード:<input type="text" name="edit_password"><br>
      <input type="submit" name="submit_edit" value="編集する">
    </form>
    <?php
     $text_file_array_display = file($text_file);
     foreach ($text_file_array_display as $display_value) {
       $display_value_explode = explode("<>",$display_value);
       echo $display_value_explode[0] . " " . $display_value_explode[2] . " " . $display_value_explode[3] . " " . $display_value_explode[4] . "<br>";
     }
     ?>
   </body>
 </html>
<?php
echo "パスワード、編集番号、削除番号は全て半角で入力してください";
$text_file = "kadai_2_6.txt";
$count_file = "kadai_2_6count.txt";
$date = date("Y-m-d");
$edit_form_number = "";
$edit_form_name = "";
$edit_form_comment = "";
$edit_form_password = "";
if (!empty($_POST["submit_edit"])) {
  $text_file_array_edit = file($text_file);
  $edit_number = $_POST["edit_number"];
  $edit_password = $_POST["edit_password"];
  foreach ($text_file_array_edit as $edit_value) {
    $edit_value_explode = explode("<>",$edit_value);
    if ($edit_value_explode[0] == $edit_number && $edit_value_explode[1] == $edit_password) {
      $edit_form_number = $edit_value_explode[0];
      $edit_form_name = $edit_value_explode[2];
      $edit_form_comment = $edit_value_explode[3];
      $edit_form_password = $edit_value_explode[1];
    }
  }
}
if (!empty($_POST["submit_normal"])) {
  $normal_name = $_POST["normal_name"];
  $normal_comment = $_POST["normal_comment"];
  $normal_password = $_POST["normal_password"];
  $fp = fopen($count_file,"r");
  $count = fgets($fp);
  fclose($fp);
  $count+=1;
  $fp = fopen($count_file,"w");
  fwrite($fp,$count);
  fclose($fp);
  $text_contents_normal = $count . "<>" . $normal_password . "<>" . $normal_name . "<>" . $normal_comment . "<>" . $date . "\n";
  $fp = fopen($text_file,"a");
  fwrite($fp,$text_contents_normal);
  fclose($fp);
  if (!empty($_POST["edit_hidden"])) {
    $text_file_array_edit_contents = file($text_file);
    $edit_hidden = $_POST["edit_hidden"];
    foreach ($text_file_array_edit_contents as &$text_contents_edit) {
      $text_contents_edit_explode = explode("<>",$text_contents_edit);
      if ($text_contents_edite_xplode[0] == $edit_hidden) {
        $text_contents_edit = $edit_hidden . "<>" . $normal_password . "<>" . $normal_name . "<>" . $normal_comment . "<>" . $date . "\n";
      }
    }
    $fp = fopen($text_file,"w");
    fwrite($fp,$text_file_array_edit_contents);
    fclose($fp);
  }
}
if (!empty($_POST["submit_delete"])) {
  $text_file_array_delete = file($text_file);
  $delete_number = $_POST["delete_number"];
  $delete_password = $_POST["delete_password"];
  $text_contents_delete = [];
  foreach ($text_file_array_delete as $delete_value) {
    $delete_value_explode = explode("<>",$delete_value);
    if ($delete_value_explode[0] == $delete_number && $delete_value_explode[1] == $delete_password) {
      continue;
    }else{
      $text_contents_delete[] = $delete_value;
    }
  }
  $fp = fopen($text_file,"w");
  fwrite($fp,implode("",$text_contents_delete));
  fclose($fp);
}
 ?>
 <!DOCTYPE html>
 <html lang="ja">
   <head>
     <meta charset="utf-8">
     <title>簡易掲示板</title>
   </head>
   <body>
     <h1>簡易掲示板</h1>
     <form action="kadai_2_6.php" method="post">
       <input type="hidden" name="edit_hidden" value="<?php echo $edit_form_number; ?>">
       名前:<input type="text" name="normal_name" value="<?php echo $edit_form_name; ?>"><br>
       コメント:<input type="text" name="normal_comment" value="<?php echo $edit_form_comment; ?>"><br>
       パスワード:<input type="text" name="normal_password" value="<?php echo $edit_form_password; ?>"><br>
       <input type="submit" name="submit_normal" value="投稿">
    </form>
    <h1>削除フォーム</h1>
    <form action="kadai_2_6.php" method="post">
      削除番号:<input type="text" name="delete_number"><br>
      パスワード:<input type="text" name="delete_password"><br>
      <input type="submit" name="submit_delete" value="削除する">
    </form>
    <h1>編集フォーム</h1>
    <form action="kadai_2_6.php" method="post">
      編集番号:<input type="text" name="edit_number"><br>
      パスワード:<input type="text" name="edit_password"><br>
      <input type="submit" name="submit_edit" value="編集する">
    </form>
    <?php
     $text_file_array_display = file($text_file);
     foreach ($text_file_array_display as $display_value) {
       $display_value_explode = explode("<>",$display_value);
       echo $display_value_explode[0] . " " . $display_value_explode[2] . " " . $display_value_explode[3] . " " . $display_value_explode[4] . "<br>";
     }
     ?>
   </body>
 </html>
No one still commented. Please first comment.
Output