-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice7.php
More file actions
65 lines (58 loc) · 1.88 KB
/
practice7.php
File metadata and controls
65 lines (58 loc) · 1.88 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/*
結局わけわからん
誰か助けて...........
わかったどぉぉぉぉx
*/
session_start();
// 仮のユーザーデータ(通常はデータベースから取得)
$valid_username = 'user';
$valid_password = 'password';
// ログイン処理
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['login'])) {
$username = $_POST['username'];
$password = $_POST['password'];
// ユーザー認証
if ($username == $valid_username && $password == $valid_password) {
// セッションにユーザー情報を保存
$_SESSION['loggedin'] = true;
$_SESSION['username'] = $username;
} else {
$error = 'Invalid username or password';
}
}
// ログアウト処理
if (isset($_GET['logout'])) {
// セッションを破棄
session_destroy();
// セッション変数をすべて解除
$_SESSION = array();
header('Location: ' . $_SERVER['PHP_SELF']);//こんなコードでリロードできたんだ..感動......
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Session Example</title>
</head>
<body>
<?php if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true): ?>
<h2>Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?>!</h2>
<p>You are logged in.</p>
<a href="?logout">Logout</a>
<?php else: ?>
<h2>Login</h2>
<?php if (isset($error)): ?>
<p style="color:red;"><?php echo $error; ?></p>
<?php endif; ?>
<form method="post" action="">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>
<button type="submit" name="login">Login</button>
</form>
<?php endif; ?>
</body>
</html>