auth.php
<?php
session_start();
if(empty($_POST["id"]) || empty($_POST["password"])){
header("Location: login.php");
exit();
}
require_once("config.php");
$sql="SELECT * FROM members WHERE id=:id";
$stmt=$pdo->prepare($sql);
$stmt->bindValue(":id",$_POST["id"],PDO::PARAM_INT);
$stmt->execute();
//取り出しただけなのでfetchする
$row=$stmt->fetch(PDO::FETCH_ASSOC);
//ハッシュ済みと送られてきたpassの比較
if(password_verify($_POST["password"],$row["password"])){
//認証成功
session_regenerate_id();//session_idをまず最初にふりかえる
$_SESSION["login"]=true;
$_SESSION["m_name"]=$row["m_name"];
$_SESSION["id"]=$row["id"];
header("Location: mypage.php");
exit();
}else{
//認証失敗
$_SESSION=[];//全消し
header("Location: login.php");
exit();
}
?>
mypage.php
<?php
session_start();
if(empty($_SESSION["login"])){
header("Location: login.php");
exit();
}
require_once("config.php");
$sql = "SELECT mails.id AS i,subject,sendtime,m_name FROM mails,members WHERE m_from=members.id AND m_to=:m_to ORDER BY sendtime DESC";
$stmt=$pdo->prepare($sql);
$stmt->bindValue("m_to",$_SESSION["id"],PDO::PARAM_INT);
$stmt->execute();
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>マイページ</title>
<link rel="stylesheet" href="css/style.css">
<script src="../jquery-3.4.1.min.js"></script>
</head>
<body>
<div id="container">
<h1><?php echo htmlspecialchars($_SESSION["m_name"],ENT_QUOTES); ?>の受信トレイ</h1>
<!--メール概要を表示-->
<div id="m_list">
<table>
<tr>
<th>件名</th><th>送信者名</th><th>送信日時</th>
</tr>
<?php while($row=$stmt->fetch(PDO::FETCH_ASSOC)): ?>
<tr>
<td data-id="<?php echo $row["i"]; ?>" class="subject"><?php echo htmlspecialchars($row["subject"],ENT_QUOTES); ?></td>
<td><?php echo htmlspecialchars($row["m_name"],ENT_QUOTES); ?></td>
<td><?php echo $row["sendtime"]; ?></td>
</tr>
<?php endwhile; ?>
</table>
</div>
<hr>
<!--メール詳細を表示-->
<div id="mail">
<table>
<tr>
<th rowspan="3">詳細</th>
<td colspan="2" id="subject"></td>
</tr>
<tr>
<td id="m_name"></td><td id="sendtime"></td>
</tr>
<tr>
<td colspan="2" id="content"></th>
</tr>
</table>
</div>
</div>
<script src="js/mail.js"></script>
</body>
</html>
style.css
body{
background:#fee;
}
#container{
border:1px #ccc solid;
border-radius:2rem;
background:#fff;
padding:1rem;
margin:0 auto;
width:80%;
}
table{
border collapse: collapse;
width:100%;
}
td,th{
border:solid 1px #ccc;
padding:0.2rem 0.5rem;
text-align: left;
}
th{
background:#eee;
}
/*mail list view and mail*/
.subject{
cursor:pointer;
}
hr{
margin: 1rem 0 1rem;
}
#m_list{
height: 230px;
}
#mail{
display:none;
}
get_mail_data.php
<?php
if(empty($_GET["i"])){
exit();//なにもしません
}
require_once("../config.php");//一つ上のディレクトリの
$sql = "SELECT * FROM mails WHERE id=:id";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(":id",$_GET["i"],PDO::PARAM_INT);
$stmt->execute();
//連想配列として取り出す
$row = $stmt->fetch(PDO::FETCH_ASSOC);//1件だけのはずなので
echo json_encode($row);
?>
mail.js
$(function(){
//console.log("hoge");
$('td.subject').on('click', function(){
var obj = $(this).data();
//console.log(obj.id);
//件名をクリックすると詳細が出現
$('#mail').css({'display':'block'});
//Ajax通信
$.ajax({
})
.done(function(d){
})
.fail(function(){
});
});
});