基礎から解る!PHP入門講座 プログラム入門に最適な言語、PHPを学ぼう

基礎から解る!PHP入門講座

プログラム入門に最適な言語、PHPを学ぼう      

    ファイル 13−2

    <?php
    error_reporting(1);
    /*------------------------------------------パスワード”の設定*/
    $pass_w = $_POST['password_w'];
      if($pass_w==null){
        $pass_w=$_COOKIE['password_k'];
      }
    setcookie("password_k", $pass_w, time()+60*60);

    $login = $_POST['loginn'];        /*ログオフの設定*/          
      if($login == "LOGOFF") {
        setcookie("password_k");      /*クッキー"password"をクリア*/
        $pass_w = "";            /*パスワードデータをクリア*/
      }
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html lang="ja"><head>
    <meta http-equiv="Content-type" content="text/html; charset=EUC-JP">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <link rel="stylesheet" href="../style.css">
    <style type="text/css">
    <!--
    .nikki {
    font-size:15px;
    line-height:15px;
    text-align:left;
    width:500px;
    padding:10px;
    margin:0px;
    border-style:solid;
    border-width:1px;
    background-color:#ffffff;
    border-color:#abcdef;
    }
    //-->
    </style>
    <title>日記帳</title></head>
    <body><div class="file2">
    <?php
    if($pass_w != "pass"){
      echo "<form action='13_02_nikki.php' method='post'>¥n";
      echo "<input type='password' name='password_w' size='12'>¥n";
      echo " <input type='submit' name ='loginn' value='LOGIN'>¥n";
      echo "</form>";
      echo "パスワードを入れてね。pass です。";
    } else if($pass_w == "pass"){
    /*------------------------------------------LOGOFFの設定*/
      echo "<form action='13_02_nikki.php' method='post'>";
      echo "<table width='500' border='0'><tr><td valign='middle'>";
      echo "<span style='margin:0px;font-size:15px'>";
      echo "日記帳 : ";
      echo date("Y/m/d") . "</span></td>¥n";
      echo "<td><input type='submit' name ='loginn' value='LOGOFF'>";
      echo "</td></tr></table>";
    /*--------------------------------------------------------------*/
      echo "<table border='0' width='500'><tr><td>";
      $year = date(Y);
      echo "<input type='text' name='year' size='5'";
      echo "value=$year>年 ";
    /*--------------------------------------------------------------*/
      echo "<select name='month'>";
      $cnt = 1;
      while ($cnt < 13){
        if($cnt == date(n)){
        echo "<option value='$cnt' selected>".$cnt."月</option>";
        } else {
        echo "<option value='$cnt'>".$cnt."月</option>";
        }
      $cnt++;
      }
      echo "</select>";
    /*--------------------------------------------------------------*/
      echo " <select name='day'>";
        $sday = date(j);
        $op = 1;
        while($op <= 31){
          if($op == $sday){
          echo "<option value='$op' selected>".$op."日</option>¥n";
          } else {
          echo "<option value='$op'>".$op."日</option>¥n";
          }
          $op++;
        }
      echo "</select>";
    /*--------------------------------------------------------------*/
      echo " <select name='weather'>";
      echo "<option value='快晴'>快晴</option>";
      echo "<option value='晴れ'>晴れ</option>";
      echo "<option value='ほぼ晴れ' selected>ほぼ晴れ</option>";
      echo "<option value='曇り'>曇り</option>";
      echo "<option value='ドン曇り'>ドン曇り</option>";
      echo "<option value='曇り/雨'>曇り/雨</option>";
      echo "<option value='雨'>雨</option>";
      echo "</select>";
    /*--------------------------------------------------------------*/
      echo " <select name='temp'>";
        $op = -10;
        while($op <= 40){
          if($op == 20){
          echo "<option value='$op 度' selected>".$op."度</option>¥n";
          } else {
          echo "<option value='$op 度'>".$op."度</option>¥n";
          }
          $op++;
        }
      echo "</select></td></tr>";
    /*--------------------------------------------------------------*/
      echo "<tr><td><textarea name='data' cols='70' rows='5'></textarea></td></tr>";
      echo "<tr><td><input type='submit' name='enter' value='書込/表示'> ";
      echo "<input type='submit' name='enter' value='消す!'></td></tr></table></form>";
      echo "月を選んで「書込/表示」をクリックすると表示されます。<br>適当に書き込んでみてください。<br><br>";
    /*-------------------------------------------------データ受取----*/
      $month = $_POST['month'];
      $day = $_POST['day'];
      $year = $_POST['year'];
      $weather = $_POST['weather'];
      $temp = $_POST['temp'];
      $data = $_POST['data'];
      $data = htmlspecialchars($data, ENT_QUOTES);
      $enter = $_POST['enter'];
      $data = str_replace("¥n", '<br>', $data);
      $data = str_replace("and", 'and', $data);  /*データの区切りに使うandは全角に変える*/
      $length = strlen($data);
      echo $enter;
      $youbimei = array("日曜日","月曜日","火曜日","水曜日","木曜日","金曜日","土曜日");
      $stamp = mktime(0,0,0,$month,$day,$year);
      $yb = date(w,$stamp);
      $youbi = $youbimei[$yb];
      if(!file_exists("diary1/$year")){
        mkdir ("diary1/$year",0777);
      }
      $updir = "diary1/$year/month".$month;
      if(!file_exists($updir)){
        mkdir ($updir,0777);
      }
    /*-------------------------------------------------ここから書き込み部分-------------*/
      if ($enter == "消す!" and $data == null){    
        if(file_exists("$updir/dairy$day")){      /*ファイルの存在を確認*/      
          unlink("$updir/dairy$day");          /*ファイルを削除*/
        }
      } else if($enter == "書込/表示" and $data != null){
          if($length < 250){
            if($updir != null){
              if(file_exists("./$updir/dairy$day")){
                echo "<font color='red'><b>その日の日記は既に記入済みです。</b></font><br><br>";
                echo "<div>$data</div>";
              } else {
                $filepointer=fopen("./$updir/dairy$day", "w+");
                  flock($filepointer, LOCK_EX);
                    fputs($filepointer, $year."and".$month."and".$day."and".$youbi."and".$weather."and".$temp."and".$data);
                  flock($filepointer, LOCK_UN);
                fclose($filepointer);
              }
            } else {
              echo "250文字以内で記述してください。";
            }
          }
      } else {
      }
    /*----------------------------------------------------ここから表示部分----------*/
      echo " ".$year."年 ".$month."月<br>";
      $count = 31;                
      while($count > 0) {
        if(file_exists("./$updir/dairy$count")){
          $filepointer=fopen("./$updir/dairy$count", "r");
            while(!feof($filepointer)){    /*この場合は¥nを<br>に変換してあるので必要ない*/
             $fileline = fgets($filepointer);
                $text = split("and",$fileline);
                echo "<td align='center' rowspan='2' valign='middle' bgcolor='#ffffff'>";
                echo "<div class='nikki'>";
                echo "<b>$text[1]月$text[2]日 $text[3]</b> <font color=blue>$text[4] $text[5]</font><br>";
                echo "$text[6]</div>";
            }
          fclose($filepointer);
        }
      $count--;
      }
    }
    ?>

    </div></body></html>
    2019/9
    SUN MON TUE WED THU FRI SAT
    1 2 3 4 5 6 7
    8 9 10 11 12 13 14
    15 16 17 18 19 20 21
    22 23 24 25 26 27 28
    29 30      


     フォクすけ©2006 Mozilla Japan



    制作:基礎から解るPHP入門講座 代表:FREEZE

    Copyright © 2005/2/28 flzPHP.All Rights Riserved