teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

ソースを追加しました。

2018/08/09 06:59

投稿

TadashiOsanai
TadashiOsanai

スコア10

title CHANGED
File without changes
body CHANGED
@@ -32,4 +32,160 @@
32
32
  exit();
33
33
  ```
34
34
  ご教授いただけないでしょうか?
35
- よろしくお願いします。
35
+ よろしくお願いします。
36
+
37
+
38
+ login.php
39
+ ```ここに言語を入力
40
+ <?php
41
+ session_start();
42
+
43
+ header("Content-type: text/html; charset=utf-8");
44
+
45
+
46
+ ?>
47
+
48
+ <!DOCTYPE html>
49
+ <html>
50
+ <head>
51
+ <title>ログイン画面</title>
52
+ <meta charset="utf-8">
53
+ </head>
54
+ <body>
55
+ <h1>ログイン画面</h1>
56
+
57
+ <form action="login_check.php" method="post">
58
+
59
+ <p>アカウント:<input type="text" name="account" size="50"></p>
60
+ <p>パスワード:<input type="text" name="password" size="50"></p>
61
+
62
+ <input type="hidden" name="token" value="<?=$token?>">
63
+ <input type="submit" value="ログインする">
64
+
65
+ </form>
66
+
67
+ </body>
68
+ </html>
69
+ ```
70
+
71
+ login_check.php
72
+ ```ここに言語を入力
73
+ <?php
74
+ session_start();
75
+
76
+ header("Content-type: text/html; charset=utf-8");
77
+
78
+
79
+ //データベース接続
80
+ require_once("db.php");
81
+ $dbh = db_connect();
82
+
83
+ //前後にある半角全角スペースを削除する関数
84
+ function spaceTrim ($str) {
85
+ // 行頭
86
+ $str = preg_replace('/^[  ]+/u', '', $str);
87
+ // 末尾
88
+ $str = preg_replace('/[  ]+$/u', '', $str);
89
+ return $str;
90
+ }
91
+
92
+ //エラーメッセージの初期化
93
+ $errors = array();
94
+
95
+ if(empty($_POST)) {
96
+ header("Location: index.php");
97
+ exit();
98
+ }else{
99
+ //POSTされたデータを各変数に入れる
100
+ $mail = isset($_POST['mail']) ? $_POST['mail'] : NULL;
101
+ $password = isset($_POST['password']) ? $_POST['password'] : NULL;
102
+
103
+ //前後にある半角全角スペースを削除
104
+ $mail = spaceTrim($mail);
105
+ $password = spaceTrim($password);
106
+
107
+ //アカウント入力判定
108
+ if ($mail == ''):
109
+ $errors['mail'] = "アカウントが入力されていません。";
110
+ elseif(mb_strlen($mail)>50):
111
+ $errors['mail_length'] = "アカウントは50文字以内で入力して下さい。";
112
+ endif;
113
+
114
+ //パスワード入力判定
115
+ if ($password == ''):
116
+ $errors['password'] = "パスワードが入力されていません。";
117
+ elseif(!preg_match('/^[0-9a-zA-Z]{5,30}$/', $_POST["password"])):
118
+ $errors['password_length'] = "パスワードは半角英数字の5文字以上30文字以下で入力して下さい。";
119
+ else:
120
+ $password_hide = str_repeat('*', strlen($password));
121
+ endif;
122
+
123
+ }
124
+
125
+ //エラーが無ければ実行する
126
+ if(count($errors) === 0){
127
+ try{
128
+ //例外処理を投げる(スロー)ようにする
129
+ $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
130
+
131
+ //アカウントで検索
132
+ $statement = $dbh->prepare("SELECT * FROM member WHERE mail=(:mail) AND flag =1");
133
+ $statement->bindValue(':mail', $mail, PDO::PARAM_STR);
134
+ $statement->execute();
135
+
136
+ //アカウントが一致
137
+ if($row = $statement->fetch()){
138
+
139
+ $password_hash = $row[password];
140
+
141
+ //パスワードが一致
142
+ if (password_verify($password, $password_hash)) {
143
+
144
+ //セッションハイジャック対策
145
+ session_regenerate_id(true);
146
+
147
+ $_SESSION['mail'] = $mail;
148
+ header("Location: test.php");
149
+ exit();
150
+ }else{
151
+ $errors['password'] = "メールアドレス及びパスワードが一致しません。";
152
+ }
153
+ }else{
154
+ $errors['mail'] = "メールアドレス及びパスワードが一致しません。";
155
+ }
156
+
157
+ //データベース接続切断
158
+ $dbh = null;
159
+
160
+ }catch (PDOException $e){
161
+ print('Error:'.$e->getMessage());
162
+ die();
163
+ }
164
+ }
165
+
166
+ ?>
167
+
168
+ <!DOCTYPE html>
169
+ <html>
170
+ <head>
171
+ <title>ログイン確認画面</title>
172
+ <meta charset="utf-8">
173
+ </head>
174
+ <body>
175
+ <h1>ログイン確認画面</h1>
176
+
177
+ <?php if(count($errors) > 0): ?>
178
+
179
+ <?php
180
+ foreach($errors as $value){
181
+ echo "<p>".$value."</p>";
182
+ }
183
+ ?>
184
+
185
+ <input type="button" value="戻る" onClick="history.back()">
186
+
187
+ <?php endif; ?>
188
+
189
+ </body>
190
+ </html>
191
+ ```