忍者ブログ

からすまる日誌

今日の最後のコード

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

今日の最後のコード

つづく。
月曜にはログアウトといいねとアイコンを追加実装予定。


index.php(ツイッターメイン画面)

 
<?php
session_start();
 
//共通部分読み込み
require_once("config.php");
 
$sql="SELECT nickname,tweet,created FROM tweets,users WHERE tweets.u_id=users.u_id ORDER BY t_id DESC";
$res=$pdo->query($sql);
?>
<!DOCTYPE html>
<html lang="ja">
 <head>
  <meta charset="UTF-8">
  <title>Twitter タイムライン</title>
  <link rel="stylesheet" href="css/style.css">
 </head>
 <body>
  <div id="container">
   <h1>My Twitter</h1>
   
   <!--セッション変数のloginがからっぽでない場合は投稿フォーム出現-->
   <?php if(!empty($_SESSION["login"])): ?>
    <div id="tweetform">
     <form method="post" action="exec.php">
     <textarea name="tweet" rows="2" cols="50"></textarea>
     <!--<input type="text" name="tweet">-->
     <button type="submit">ツイートする<img src="img/player_button08_plus.png"></button>
    </div>
 
   <?php else: ?>
    <!--そうでなければログイン画面へのリンクを追加表示-->
    <div><a href="login.php">ログインする</a></div>
   <?php endif; ?>
    
   <!--既にあるツイートをあるだけ表示-->
   <?php while($row = $res->fetch(PDO::FETCH_ASSOC)): ?>
   <article>
    <header>
     <?php echo htmlspecialchars($row["nickname"],ENT_QUOTES); ?>
    </header>
    <p><?php echo htmlspecialchars($row["tweet"],ENT_QUOTES); ?></p>
    <div><?php echo $row["created"]; ?></div>
   </article>
   <?php endwhile; ?>
   
  </div>
  
 </body>
</html>


style.css(cssフォルダ内)
 
/*
body{
 background-image: url("../../img/fish_kue2.png");
 color:#333;
}
*/
#container{
 width:80%;
 margin: auto;
 background:#fff;
}
table{
 border-collapse:collapse;
}
th,td{
 border:solid 1px #333;
 padding: 0.2rem 0.3rem;
 text-align: left;
}
th{
 background:#eee;
}
h1{
 border-left:10px solid #ffa50a;
 /*border-radius:2rem;*/
 padding:0.5rem;
 color:#ffa50a;
}
/*タイムライン*/
article{
 margin:2rem;
 border-bottom:dotted 2px #ffa50a;
}
article p{
 margin:0.5rem 0;
}
article header{
 font-weight:bold;
 font-size:0.8rem;
}
article div{
 color:#333;
 font-size:0.8rem;
 text-align:right;
}
#tweetform img{
 width:30px;
}
#tweetform textarea{
 font-size:1.2rem;
 padding:0.3rem;
 border-radius:0.2rem;
}


config.php(共通部分の切りだし/db接続部分)
 
<?php
//--------------------------
//DB接続設定
//環境に合わせて再定義のこと
//--------------------------
$user="root";
$dbpass="";
$host="localhost";
$dbname="twitter";
//--------------------------
$dsn="mysql:host={$host};dbname={$dbname};charset=utf8";
$pdo=new PDO($dsn,$user,$dbpass);
?>


auth.php(認証処理/非表示)
 
<?php
session_start();
//評価するデータが来ているかいないか
if(empty($_POST["u_id"]) || empty($_POST["pass"])){
 header("Location: login.php");
 exit();
}
 
//DBへ接続
require_once("config.php");

 
//SQL文
$sql="SELECT * FROM users WHERE u_id=:u_id";//一回全部読んでくる
$stmt=$pdo->prepare($sql);//構文を焼く。こんがり。
$stmt->bindValue(":u_id",$_POST["u_id"],PDO::PARAM_STR);
$stmt->execute();

 
//FETCH。房をばらして連想配列にするために$rowに入れる
$row=$stmt->fetch(PDO::FETCH_ASSOC);//1件とりだし

 
//認証
if(password_verify($_POST["pass"],$row["pass"])){
 $_SESSION["login"] = true;
 $_SESSION["u_id"]=$row["u_id"]; //=$_POST["u_id"]でも構わない
 $_SESSION["nickname"]=$row["nickname"];
 header("Location: index.php"); //リダイレクト
 exit();
}else{
 $_SESSION["login"]=false;//ログイン失敗
 header("Location: login.php");
 exit();
}
?>


login.php(ログイン画面)
 
<!DOCTYPE html>
<html lang="ja">
 <head>
  <meta charset="utf-8">
  <title>ログインフォーム</title>
  <link rel="stylesheet" href="css/style.css">
 </head>
 <body>
  <div id="container">
   <h1>ユーザーログイン</h1>
   <form method="post" action="auth.php">
    <table>
     <tr>
      <th><label for="u_id">ユーザ名</label></th>
      <td><input type="text" name="u_id" id="u_id"></td>
     </tr>
     <tr>
      <th><label for="pass">パスワード</label></th>
      <td><input type="password" name="pass" id="pass"></td>
     </tr>
     
    </table>
    <p><button type="submit">認証</button></p>
    
   </form>
  </div>
 </body>
</html>


exec.php(ツイート追加処理/非表示)
 
<?php
session_start();
//セッション情報がないかfalse
if(empty($_SESSION["login"])){
 header("Location: login.php");
 exit();
}
//tweetがからっぽ
if(empty($_POST["tweet"])){
 header("Location: index.php");
 exit();
}
require_once("config.php");
$sql="INSERT INTO tweets(u_id,tweet) VALUES(:u_id,:tweet)"; //プレースホルダを使う
$stmt=$pdo->prepare($sql);
$stmt->bindValue(":u_id",$_SESSION["u_id"],PDO::PARAM_STR);
$stmt->bindValue(":tweet",$_POST["tweet"],PDO::PARAM_STR);
$stmt->execute();
header("Location: index.php");
exit();
?>
PR

コメント

ブログ内検索

カレンダー

03 2025/04 05
S M T W T F S
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