matching.php
<?php
session_start();
if(empty($_SESSION['USER'])){
header('Location: login.php');
exit;
}
if(isset($_POST['logout'])){
$_SESSION = [];
session_destroy;
header('Location: login.php');
exit;
}
if(isset($_POST['submit'])){
$fp = fopen($_SESSION['State'].'.csv', 'a+b');
if($_POST['comment'] == "allclear"){
fclose($fp);
unlink($_SESSION['State'].'.csv');
}else{
if (($_SERVER['REQUEST_METHOD'] === 'POST') && $_POST['comment']) {
fputcsv($fp, [$_SESSION['USER'], $_SESSION['AGE'], $_SESSION['SEX'], $_POST['comment']]);
rewind($fp);
}else{
}
fclose($fp);
}
}
$fp = fopen($_SESSION['State'].'.csv', 'a+b');
while ($row = fgetcsv($fp)) {
$rows[] = $row;
}
if(!empty($rows)) $rows = array_reverse($rows);
fclose($fp);
if(isset($_POST['back'])){
$_SESSION['State'] =[];
header('Location: startpage.php');
exit;
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<link href="matching.css" rel="stylesheet" type="text/css" media="all">
<title><?=$_SESSION['State']?></title>
</head>
<body>
<hr><hr>
<h1 class="center" id="pink"><?=$_SESSION['State']?></h1>
<p><?=$_SESSION["USER"]?>さんでログイン中</p>
<form action="matching.php" method="post"><input type=submit name="logout" value="ログアウト"></form>
<hr><hr>
<section class="center">
<p>
<?=$_SESSION["USER"]?>(<?=$_SESSION['AGE']?>)
<?php if($_SESSION["SEX"] == "m"): ?>
<span id="male">男</span>
<?php elseif($_SESSION["SEX"] == "f"): ?>
<span id="female">女</span>
<?php else: ?>
<span id="other">その他</span>
<?php endif; ?>
</p>
<form action="" method="post">
<div class="margin"><span class="top">本文:</span><textarea name="comment" cols="35" rows="3" maxlength="200" wrap="soft" placeholder="募集するメッセージを入力してください。"></textarea></div>
<input type="submit" value="投稿" name="submit">
</form>
<form action="matching.php" method="post">
<input type ="submit" value ="退出" name="back">
</form>
</section>
<hr><hr>
<section>
<h2 class="center">投稿一覧</h2>
<div class="left">
<?php if (!empty($rows)): ?>
<?php foreach ($rows as $row): ?>
<hr>
<?php if($row[2] == "m"): ?>
<div class="contribution">
<p><?=$row[0]?>(<?=$row[1]?>) <span id="male">男</span></p>
<p><?=$row[3]?></p>
</div>
<?php elseif($row[2] == "f"): ?>
<div class="contribution">
<p><?=$row[0]?>(<?=$row[1]?>) <span id="female">女</span></p>
<p><?=$row[3]?></p>
</div>
<?php else: ?>
<div class="contribution">
<p><?=$row[0]?>(<?=$row[1]?>) <span id="other">その他</span></p>
<p><?=$row[3]?></p>
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php else: ?>
<p>投稿はまだありません</p>
<?php endif; ?>
</div>
</section>
</body>
</html>