質問編集履歴

1

ソースを追加しました。

2018/08/09 06:59

投稿

TadashiOsanai
TadashiOsanai

スコア10

test CHANGED
File without changes
test CHANGED
@@ -67,3 +67,315 @@
67
67
  ご教授いただけないでしょうか?
68
68
 
69
69
  よろしくお願いします。
70
+
71
+
72
+
73
+
74
+
75
+ login.php
76
+
77
+ ```ここに言語を入力
78
+
79
+ <?php
80
+
81
+ session_start();
82
+
83
+
84
+
85
+ header("Content-type: text/html; charset=utf-8");
86
+
87
+
88
+
89
+
90
+
91
+ ?>
92
+
93
+
94
+
95
+ <!DOCTYPE html>
96
+
97
+ <html>
98
+
99
+ <head>
100
+
101
+ <title>ログイン画面</title>
102
+
103
+ <meta charset="utf-8">
104
+
105
+ </head>
106
+
107
+ <body>
108
+
109
+ <h1>ログイン画面</h1>
110
+
111
+
112
+
113
+ <form action="login_check.php" method="post">
114
+
115
+
116
+
117
+ <p>アカウント:<input type="text" name="account" size="50"></p>
118
+
119
+ <p>パスワード:<input type="text" name="password" size="50"></p>
120
+
121
+
122
+
123
+ <input type="hidden" name="token" value="<?=$token?>">
124
+
125
+ <input type="submit" value="ログインする">
126
+
127
+
128
+
129
+ </form>
130
+
131
+
132
+
133
+ </body>
134
+
135
+ </html>
136
+
137
+ ```
138
+
139
+
140
+
141
+ login_check.php
142
+
143
+ ```ここに言語を入力
144
+
145
+ <?php
146
+
147
+ session_start();
148
+
149
+
150
+
151
+ header("Content-type: text/html; charset=utf-8");
152
+
153
+
154
+
155
+
156
+
157
+ //データベース接続
158
+
159
+ require_once("db.php");
160
+
161
+ $dbh = db_connect();
162
+
163
+
164
+
165
+ //前後にある半角全角スペースを削除する関数
166
+
167
+ function spaceTrim ($str) {
168
+
169
+ // 行頭
170
+
171
+ $str = preg_replace('/^[  ]+/u', '', $str);
172
+
173
+ // 末尾
174
+
175
+ $str = preg_replace('/[  ]+$/u', '', $str);
176
+
177
+ return $str;
178
+
179
+ }
180
+
181
+
182
+
183
+ //エラーメッセージの初期化
184
+
185
+ $errors = array();
186
+
187
+
188
+
189
+ if(empty($_POST)) {
190
+
191
+ header("Location: index.php");
192
+
193
+ exit();
194
+
195
+ }else{
196
+
197
+ //POSTされたデータを各変数に入れる
198
+
199
+ $mail = isset($_POST['mail']) ? $_POST['mail'] : NULL;
200
+
201
+ $password = isset($_POST['password']) ? $_POST['password'] : NULL;
202
+
203
+
204
+
205
+ //前後にある半角全角スペースを削除
206
+
207
+ $mail = spaceTrim($mail);
208
+
209
+ $password = spaceTrim($password);
210
+
211
+
212
+
213
+ //アカウント入力判定
214
+
215
+ if ($mail == ''):
216
+
217
+ $errors['mail'] = "アカウントが入力されていません。";
218
+
219
+ elseif(mb_strlen($mail)>50):
220
+
221
+ $errors['mail_length'] = "アカウントは50文字以内で入力して下さい。";
222
+
223
+ endif;
224
+
225
+
226
+
227
+ //パスワード入力判定
228
+
229
+ if ($password == ''):
230
+
231
+ $errors['password'] = "パスワードが入力されていません。";
232
+
233
+ elseif(!preg_match('/^[0-9a-zA-Z]{5,30}$/', $_POST["password"])):
234
+
235
+ $errors['password_length'] = "パスワードは半角英数字の5文字以上30文字以下で入力して下さい。";
236
+
237
+ else:
238
+
239
+ $password_hide = str_repeat('*', strlen($password));
240
+
241
+ endif;
242
+
243
+
244
+
245
+ }
246
+
247
+
248
+
249
+ //エラーが無ければ実行する
250
+
251
+ if(count($errors) === 0){
252
+
253
+ try{
254
+
255
+ //例外処理を投げる(スロー)ようにする
256
+
257
+ $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
258
+
259
+
260
+
261
+ //アカウントで検索
262
+
263
+ $statement = $dbh->prepare("SELECT * FROM member WHERE mail=(:mail) AND flag =1");
264
+
265
+ $statement->bindValue(':mail', $mail, PDO::PARAM_STR);
266
+
267
+ $statement->execute();
268
+
269
+
270
+
271
+ //アカウントが一致
272
+
273
+ if($row = $statement->fetch()){
274
+
275
+
276
+
277
+ $password_hash = $row[password];
278
+
279
+
280
+
281
+ //パスワードが一致
282
+
283
+ if (password_verify($password, $password_hash)) {
284
+
285
+
286
+
287
+ //セッションハイジャック対策
288
+
289
+ session_regenerate_id(true);
290
+
291
+
292
+
293
+ $_SESSION['mail'] = $mail;
294
+
295
+ header("Location: test.php");
296
+
297
+ exit();
298
+
299
+ }else{
300
+
301
+ $errors['password'] = "メールアドレス及びパスワードが一致しません。";
302
+
303
+ }
304
+
305
+ }else{
306
+
307
+ $errors['mail'] = "メールアドレス及びパスワードが一致しません。";
308
+
309
+ }
310
+
311
+
312
+
313
+ //データベース接続切断
314
+
315
+ $dbh = null;
316
+
317
+
318
+
319
+ }catch (PDOException $e){
320
+
321
+ print('Error:'.$e->getMessage());
322
+
323
+ die();
324
+
325
+ }
326
+
327
+ }
328
+
329
+
330
+
331
+ ?>
332
+
333
+
334
+
335
+ <!DOCTYPE html>
336
+
337
+ <html>
338
+
339
+ <head>
340
+
341
+ <title>ログイン確認画面</title>
342
+
343
+ <meta charset="utf-8">
344
+
345
+ </head>
346
+
347
+ <body>
348
+
349
+ <h1>ログイン確認画面</h1>
350
+
351
+
352
+
353
+ <?php if(count($errors) > 0): ?>
354
+
355
+
356
+
357
+ <?php
358
+
359
+ foreach($errors as $value){
360
+
361
+ echo "<p>".$value."</p>";
362
+
363
+ }
364
+
365
+ ?>
366
+
367
+
368
+
369
+ <input type="button" value="戻る" onClick="history.back()">
370
+
371
+
372
+
373
+ <?php endif; ?>
374
+
375
+
376
+
377
+ </body>
378
+
379
+ </html>
380
+
381
+ ```