-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_screen_login.php
100 lines (88 loc) · 3.2 KB
/
admin_screen_login.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
<?php
#require 'password.php';
// セッション開始
session_start();
$db['host'] = "localhost"; // DBサーバのurl
$db['user'] = "dbuser";
$db['pass'] = "password";
$db['dbname'] = "test_db";
// エラーメッセージの初期化
$errorMessage = "";
// ログインボタンが押された場合
if (isset($_POST["login"])) {
// 1.ユーザIDの入力チェック
if (empty($_POST["userid"])) {
$errorMessage = "ユーザIDが未入力です。";
} else if (empty($_POST["password"])) {
$errorMessage = "パスワードが未入力です。";
}
// 2.ユーザIDとパスワードが入力されていたら認証する
if (!empty($_POST["userid"]) && !empty($_POST["password"])) {
// mysqlへの接続
$mysqli = new mysqli($db['host'], $db['user'], $db['pass']);
if ($mysqli->connect_errno) {
print('<p>データベースへの接続に失敗しました。</p>' . $mysqli->connect_error);
exit();
}
// データベースの選択
$mysqli->select_db($db['dbname']);
// 入力値のサニタイズ
$userid = $mysqli->real_escape_string($_POST["userid"]);
// クエリの実行
$query = "SELECT * FROM users WHERE name = '" . $userid . "'";
$result = $mysqli->query($query);
if (!$result) {
print('クエリーが失敗しました。' . $mysqli->error);
$mysqli->close();
exit();
}
while ($row = $result->fetch_assoc()) {
// パスワード(暗号化済み)の取り出し
$db_hashed_pwd = $row['password'];
}
// データベースの切断
$mysqli->close();
// 3.画面から入力されたパスワードとデータベースから取得したパスワードのハッシュを比較します。
if ($_POST["password"] == nishio) {
// if (password_verify($_POST["password"], $db_hashed_pwd)) {
// 4.認証成功なら、セッションIDを新規に発行する
session_regenerate_id(true);
$_SESSION["USERID"] = $_POST["userid"];
header("Location: admin_screen_1.php");
exit;
}
else {
// 認証失敗
$errorMessage = "ユーザIDあるいはパスワードに誤りがあります。";
}
} else {
// 未入力なら何もしない
}
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>SIZKANIC 管理者用ページ</title>
<link rel="stylesheet" href="admin_screen_1.css" type="text/css">
</head>
<body>
<h1 id="d1">ログイン画面</h1>
<div id="d2">
<form id="loginForm" name="loginForm" action="" method="POST">
<fieldset>
<legend><h2> ログインフォーム</h2></legend>
<div><?php echo $errorMessage ?></div>
<label for="userid">ユーザID</label><input type="text" id="userid" name="userid" value="<?php echo htmlspecialchars($_POST["userid"], ENT_QUOTES); ?>">
<br>
<label for="password">パスワード</label><input type="password" id="password" name="password" value="">
<br>
<input type="submit" id="login" name="login" value="ログイン">
</fieldset>
</form>
</div>
</body>
</html>