-
Notifications
You must be signed in to change notification settings - Fork 0
/
007.php
120 lines (102 loc) · 4.6 KB
/
007.php
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>第七週-Session</title>
<link href="https://fonts.googleapis.com/css?family=Noto+Sans+TC&display=swap" rel="stylesheet">
<!-- load custom.css -->
<link rel="stylesheet" href="custom.css?<?=time();?>">
</head>
<body>
<div class="container">
<?php
//save the accounts with name, student ID and password in an 2d array
$accounts = array(
array("name" => "黃一", "studentID" => "9923701", "password" => "1073299"),
array("name" => "吳二", "studentID" => "9923702", "password" => "2073299"),
);
// if the user is not logged in
if (!isset($_SESSION['login'])){
if (isset($_POST['studentID']) && isset($_POST['password'])){
// if user is try to login
// check if the student ID and password is correct
foreach ($accounts as $account){
if ($account['studentID'] == $_POST['studentID'] && $account['password'] == $_POST['password'] && $_POST['name'] == $account['name']){
// if the student ID and password is correct, set the session
$_SESSION['login'] = true;
$_SESSION['studentID'] = $account['studentID'];
$_SESSION['name'] = $account['name'];
// refresh the page
// unset session "failed"
unset($_SESSION['failed']);
header("Location: 007.php");
exit();
echo "登入成功";
}
}
// if the student ID and password is not correct, show the error message
$_SESSION['Failed'] = true;
$_SESSION['studentID'] = $_POST['studentID'];
$_SESSION['name'] = $_POST['name'];
// refresh the page
header("Location: 007.php");
exit();
echo "登入失敗";
}
// if user not login , redirect to login page
if (@$_SESSION['Failed'] != true){
?>
<form method="post" action="/007.php" class="form">
<!-- 學號 -->
<label for="studentID">學號</label>
<input type="text" id="studentID" name="studentID" placeholder="請輸入學號" required>
<!-- 姓名 -->
<label for="name">姓名</label>
<input type="text" id="name" name="name" placeholder="請輸入姓名" required>
<!-- 密碼 -->
<label for="password">密碼</label>
<input type="password" id="password" name="password" placeholder="請輸入密碼" required>
<!-- 登入 -->
<input type="submit" value="登入">
<!-- 重設 -->
<input type="reset" value="重設">
</form>
<?php
}
}
if(isset($_POST['logout'])){
echo "登出成功";
session_destroy();
header("Location: 007.php");
exit();
}
if (@$_SESSION['Failed'] && @$_SESSION['login'] == false){
// if the user is not logged in and the login is failed, show the error message
// print_r($_SESSION);
?>
<h2>登入失敗</h2>
<p>學號:<?php echo $_SESSION['studentID'] ?></p>
<p>姓名:<?php echo $_SESSION['name'] ?></p><br/>
<br/>
<!-- logout button -->
<form method="post" action="/007.php" class="form">
<input type="submit" name="logout" value="回系統登入畫面">
</form>
<?php
}else if (isset($_SESSION['login'] )){
// if user is logged in, show the content
?>
<h2>登入成功</h2>
<p>學號:<?php echo $_SESSION['studentID'] ?></p>
<p>姓名:<?php echo $_SESSION['name'] ?></p><br/>
<p>!系統登入成功!</p>
<!-- logout button -->
<form method="post" action="/007.php" class="form">
<input type="submit" name="logout" value="回系統登入畫面">
</form>
<?php
}
?>
</div>
</body>