-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration.php
executable file
·142 lines (122 loc) · 4.88 KB
/
registration.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
require_once __DIR__ . '/library/php/auth.php';
require_once __DIR__ . '/library/php/utils.php';
require_once __DIR__ . '/library/php/DbManager.php';
// 登録完了フラグ
$ok = false;
// POSTメソッドのときのみ実行
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// エラー保持用変数
$errors = [];
// ユーザから受け取ったユーザ名とパスワードを取得
$username = isset($_POST['username']) ? e($_POST['username']) : false;
$password= isset($_POST['password']) ? e($_POST['password']) : false;
$re_password= isset($_POST['re_password']) ? e($_POST['re_password']) : false;
// ユーザー名/パスワードのペアが入力されているか
$input_flag = $username && $password && $re_password;
if (!$input_flag) {
$errors[] = '入力されていない項目があります.';
}
// 同じパスワードが入力されているか
if ($input_flag && !($password === $re_password)) {
$errors[] = '入力されたパスワードが一致しません.';
}
// 生成されたトークンを取得
$token= isset($_POST['token']) ? e($_POST['token']) : false;
if (!validate_token($token)) {
$errors[] = '不正なアクセスです.';
}
// エラーがない場合はデータベースアクセス
if (count($errors) === 0) {
try {
// データベースへの接続を確立
$db = getDb();
// パスワードのハッシュ値を計算
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
// データベースに登録
$stmt = $db->prepare('INSERT INTO `users` (`username`, `password`) VALUES (:username, :password)');
$stmt->bindValue(':username', $username);
$stmt->bindValue(':password', $hashed_password);
$stmt->execute();
} catch (PDOException $e) {
// 使用されているユーザー名
if ($e->getCode() === '23000') {
$errors[] = 'Usernameが既に使用されています.';
}
} finally {
$db = null;
}
}
// エラーが発生した場合
if (count($errors) !== 0) {
//「403 Forbidden」を送信
http_response_code(403);
} else {
$ok = true;
}
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<title>会員登録</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" type="text/css" href="./library/css/bootstrap.min.css">
<!-- JavaScript -->
<script src="./library/js/jquery-3.2.1.min.js"></script>
<script src="./library/js/popper.js"></script>
<script src="./library/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row" style="height: 60px;"></div>
<div class="row">
<div class="container-fluid">
<div class="row">
<h1 class="mx-auto">ユーザー登録</h1>
</div>
<div class="row justify-content-center"><p>ユーザー情報を入力して下さい.</p></div>
<div class="row justify-content-center mt-3">
<form class="w-100" method="post" action="">
<div class="form-group row w-50 mx-auto">
<label for="inputUsername" class="col-md-3 col-form-label">Username</label>
<div class="col-md-9">
<input type="text" name="username" class="form-control" id="inputUsername" placeholder="Enter Username">
</div>
</div>
<div class="form-group row w-50 mx-auto">
<label for="inputPassword" class="col-md-3 col-form-label">Password</label>
<div class="col-md-9">
<input type="password" name="password" class="form-control" id="inputPassword" placeholder="Enter Password">
</div>
</div>
<div class="form-group row w-50 mx-auto">
<label for="inputReEnterPassword" class="col-md-3 col-form-label align-top">Validation</label>
<div class="col-md-9">
<input type="password" name="re_password" class="form-control" id="inputReEnterPassword" placeholder="Re-Enter Password">
</div>
</div>
<input type="hidden" name="token" value="<?= e(generate_token()) ?>">
<div style="text-align: center; margin-top: 20px;"><button type="submit" class="btn btn-primary">登録</button></div>
</form>
</div>
<div class="row justify-content-center mt-3 text-danger">
<?php
if (http_response_code() === 403) {
foreach($errors as $e) {
print "<p>{$e}</p>";
}
} else if ($ok) {
print "<p>登録が完了しました.</p>";
}
?>
</div>
<div class="row justify-content-center">
<a href="http://<?= e($_SERVER['HTTP_HOST']) ?>/index.php">戻る</a>
</div>
</div>
</div>
</body>
</html>