startpage.php
<?php
session_start();
$massage="";
$result=[];
date_default_timezone_set("Asia/Tokyo");
$fp = fopen("topiclist.csv", "a+b");
while ($row = fgetcsv($fp)) {
$rows[] = $row;
$titles[] = $row[0];
}
if(!empty($rows)) $rows = array_reverse($rows);
if(empty($_SESSION['USER'])){
header('Location: login.php');
exit;
}
if(isset($_POST['logout'])){
$_SESSION = [];
session_destroy;
header('Location: login.php');
exit;
}
if(isset($_POST['makenewtopic'])){
if(!empty($_POST['newtopic'])){
if(!empty($titles)){
for($j=0;$j<=count($titles);$j++){
if(strcmp($rows[$j][0], $_POST['newtopic']) == 0){
$massage="その部屋はすでにあります";
$j= -1;
break;
}
}
if($j != -1){
touch($_POST['newtopic'].'.csv');
$_SESSION['State'] = $_POST['newtopic'];
fputcsv($fp, [$_SESSION['State'],$_SESSION['USER'], $_SESSION['AGE'], $_SESSION['SEX']]);
fclose($fp);
header("Location: matching.php");
exit;
}
}
else {
touch($_POST['newtopic'].'.csv');
$_SESSION['State'] = $_POST['newtopic'];
fputcsv($fp, [$_SESSION['State'],$_SESSION['USER'], $_SESSION['AGE'], $_SESSION['SEX']]);
fclose($fp);
header("Location: matching.php");
exit;
}
}else{
$massage="部屋のタイトルが入力されていません";
}
}
if(isset($_POST['search'])){
if(!empty($_POST['searchword'])){
if(!empty($titles)){
for($j=0;$j<=count($titles);$j++){
if(strpos($rows[$j][0], $_POST['searchword']) !== false){
$result[] = $rows[$j];
}
}
}
}
}
fclose($fp);
if(!empty($titles)){
for($j=0;$j<=count($titles);$j++){
if(isset($_POST[$titles[$j]])){
$_SESSION['State'] = $titles[$j];
header("Location: matching.php");
exit;
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link href="matching.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<p><?=date("現在時刻:H時i分")?></p>
<div>
<?=$_SESSION['USER']?>でログインしています
<form action="startpage.php" method="post">
<input type="submit" name="logout" value="ログアウト">
</form>
</div>
<p><?=$massage?></p>
<form action="startpage.php" method="post">
<div class="margin">
<input type="text" name="newtopic" value="" placeholder="ここに新しい部屋のタイトルを入力してください">
<input type="submit" name="makenewtopic" value="新規作成">
</div>
</form>
<form action="startpage.php" method="post">
<div class="margin">
<input type="text" name="searchword" value="">
<input type="submit" name="search" value="検索">
</div>
</form>
<hr>
<?php if(!empty($result)): ?>
<hr>
<?php foreach($result as $row): ?>
<form action="star\tpage.php" method = "post">
<input type ="submit" name="<?=$row[0]?>" value="入室">
<?=$row[0]?><br>
投稿者:<?=$row[1]?>(<?=$row[2]?>)
<?php if($row[3] == "m"): ?>
<span id="male">男</span>
<?php elseif($row[3] == "f"): ?>
<span id="female">女</span>
<?php else: ?>
<span id="other">その他</span>
<?php endif; ?><br>
</form>
<?php endforeach; ?>
<hr>
<?php endif; ?>
<?php if(!empty($rows)) :?>
<?php $i = 0; ?>
<?php foreach($rows as $row): ?>
<form action="startpage.php" method = "post">
<input type ="submit" name="<?=$row[0]?>" value="入室">
<?=$row[0]?><br>
投稿者:<?=$row[1]?>(<?=$row[2]?>)
<?php if($row[3] == "m"): ?>
<span id="male">男</span>
<?php elseif($row[3] == "f"): ?>
<span id="female">女</span>
<?php else: ?>
<span id="other">その他</span>
<?php endif; ?><br>
</form>
<?php endforeach; ?>
<?php else: ?>
<p>投稿がありません・新規投稿をしてください</p>
<?php endif; ?>
</body>
</html>