質問編集履歴
1
コードを追記して再現してもらいやすくしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,135 +30,639 @@
|
|
30
30
|
|
31
31
|
|
32
32
|
|
33
|
+
memo/index.php
|
34
|
+
|
35
|
+
```php
|
36
|
+
|
37
|
+
<?php
|
38
|
+
|
39
|
+
session_start(); ?>
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
<!DOCTYPE html>
|
44
|
+
|
45
|
+
<html lang="ja">
|
46
|
+
|
47
|
+
<?php
|
48
|
+
|
49
|
+
include_once "../common/header.php";
|
50
|
+
|
51
|
+
echo getHeader("ユーザー登録");
|
52
|
+
|
53
|
+
?>
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
<body>
|
58
|
+
|
59
|
+
<div class="d-flex align-items-center justify-content-center h-100">
|
60
|
+
|
61
|
+
<form action="./action/register.php" method="POST">
|
62
|
+
|
63
|
+
<div class="card rounded login-card-width shadow">
|
64
|
+
|
65
|
+
<div class="card-body">
|
66
|
+
|
67
|
+
<?php if (isset($_SESSION["errors"])) {
|
68
|
+
|
69
|
+
echo '<div class="alert alert-danger" role="alert">';
|
70
|
+
|
71
|
+
foreach ($_SESSION["errors"] as $error) {
|
72
|
+
|
73
|
+
echo "<div>{$error}</div>";
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
echo "</div>";
|
78
|
+
|
79
|
+
unset($_SESSION["errors"]);
|
80
|
+
|
81
|
+
} ?>
|
82
|
+
|
83
|
+
<div class="rounded-circle mx-auto border-gray border d-flex mt-3 icon-circle">
|
84
|
+
|
85
|
+
<img src="../public/images/animal_stand_zou.png" class="w-75 mx-auto p-2" alt="icon" />
|
86
|
+
|
87
|
+
</div>
|
88
|
+
|
89
|
+
<div class="d-flex justify-content-center">
|
90
|
+
|
91
|
+
<div class="mt-3 h2">SimpleMemo</div>
|
92
|
+
|
93
|
+
</div>
|
94
|
+
|
95
|
+
<div class="row mt-3">
|
96
|
+
|
97
|
+
<div class="offset-2 col-8 offset-2">
|
98
|
+
|
99
|
+
<label class="input-group w-100">
|
100
|
+
|
101
|
+
<span class="input-group-prepend">
|
102
|
+
|
103
|
+
<span class="input-group-text"><i class="fas fa-file-signature"></i></span>
|
104
|
+
|
105
|
+
</span>
|
106
|
+
|
107
|
+
<input type="text" name="user_name" class="form-control" placeholder="ユーザー名"
|
108
|
+
|
109
|
+
autocomplete="off" maxlength="255" />
|
110
|
+
|
111
|
+
</label>
|
112
|
+
|
113
|
+
<label class="input-group w-100">
|
114
|
+
|
115
|
+
<span class="input-group-prepend">
|
116
|
+
|
117
|
+
<span class="input-group-text"><i class="far fa-envelope"></i></span>
|
118
|
+
|
119
|
+
</span>
|
120
|
+
|
121
|
+
<input type="text" name="user_email" class="form-control" placeholder="メールアドレス"
|
122
|
+
|
123
|
+
autocomplete="off" maxlength="255" />
|
124
|
+
|
125
|
+
</label>
|
126
|
+
|
127
|
+
<label class="input-group w-100">
|
128
|
+
|
129
|
+
<span class="input-group-prepend">
|
130
|
+
|
131
|
+
<span class="input-group-text"><i class="fas fa-key"></i></span>
|
132
|
+
|
133
|
+
</span>
|
134
|
+
|
135
|
+
<input type="password" name="user_password" class="form-control" placeholder="パスワード"
|
136
|
+
|
137
|
+
autocomplete="off" maxlength="255" />
|
138
|
+
|
139
|
+
</label>
|
140
|
+
|
141
|
+
<button type="submit" class="form-control btn btn-success">
|
142
|
+
|
143
|
+
登録する
|
144
|
+
|
145
|
+
</button>
|
146
|
+
|
147
|
+
</div>
|
148
|
+
|
149
|
+
</div>
|
150
|
+
|
151
|
+
</div>
|
152
|
+
|
153
|
+
</div>
|
154
|
+
|
155
|
+
</form>
|
156
|
+
|
157
|
+
</div>
|
158
|
+
|
159
|
+
</body>
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
</html>
|
164
|
+
|
165
|
+
```
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
user/action/register.php
|
170
|
+
|
33
171
|
```php
|
34
172
|
|
35
173
|
<?php
|
36
174
|
|
37
|
-
session_start();
|
175
|
+
session_start();
|
176
|
+
|
38
|
-
|
177
|
+
require "../../common/validation.php";
|
178
|
+
|
39
|
-
|
179
|
+
require "../../common/database.php";
|
180
|
+
|
181
|
+
|
182
|
+
|
40
|
-
|
183
|
+
// パラメータ取得
|
184
|
+
|
185
|
+
$user_name = $_POST["user_name"];
|
186
|
+
|
187
|
+
$user_email = $_POST["user_email"];
|
188
|
+
|
189
|
+
$user_password = $_POST["user_password"];
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
// バリデーション
|
194
|
+
|
195
|
+
$_SESSION["errors"] = [];
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
// - 空チェック
|
200
|
+
|
201
|
+
emptyCheck($_SESSION["errors"], $user_name, "ユーザー名を入力してください。");
|
202
|
+
|
203
|
+
emptyCheck(
|
204
|
+
|
205
|
+
$_SESSION["errors"],
|
206
|
+
|
207
|
+
$user_email,
|
208
|
+
|
209
|
+
"メールアドレスを入力してください。"
|
210
|
+
|
211
|
+
);
|
212
|
+
|
213
|
+
emptyCheck(
|
214
|
+
|
215
|
+
$_SESSION["errors"],
|
216
|
+
|
217
|
+
$user_password,
|
218
|
+
|
41
|
-
|
219
|
+
"パスワードを入力してください。"
|
220
|
+
|
42
|
-
|
221
|
+
);
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
// - 文字数チェック
|
226
|
+
|
227
|
+
stringMaxSizeCheck(
|
228
|
+
|
229
|
+
$_SESSION["errors"],
|
230
|
+
|
231
|
+
$user_name,
|
232
|
+
|
233
|
+
"ユーザー名は255文字以内で入力してください。"
|
234
|
+
|
235
|
+
);
|
236
|
+
|
237
|
+
stringMaxSizeCheck(
|
238
|
+
|
239
|
+
$_SESSION["errors"],
|
240
|
+
|
241
|
+
$user_email,
|
242
|
+
|
243
|
+
"メールアドレスは255文字以内で入力してください。"
|
244
|
+
|
245
|
+
);
|
246
|
+
|
247
|
+
stringMaxSizeCheck(
|
248
|
+
|
249
|
+
$_SESSION["errors"],
|
250
|
+
|
251
|
+
$user_password,
|
252
|
+
|
253
|
+
"パスワードは255文字以内で入力してください。"
|
254
|
+
|
255
|
+
);
|
256
|
+
|
257
|
+
stringMaxSizeCheck(
|
258
|
+
|
259
|
+
$_SESSION["errors"],
|
260
|
+
|
261
|
+
$user_password,
|
262
|
+
|
263
|
+
"パスワードは8文字以上で入力してください。"
|
264
|
+
|
265
|
+
);
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
if (!$_SESSION["errors"]) {
|
270
|
+
|
271
|
+
// - メールアドレスチェック
|
272
|
+
|
273
|
+
mailAddressCheck(
|
274
|
+
|
275
|
+
$_SESSION["errors"],
|
276
|
+
|
277
|
+
$user_email,
|
278
|
+
|
279
|
+
"正しいメールアドレスを入力してください。"
|
280
|
+
|
281
|
+
);
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
// - ユーザー名・パスワード半角英数チェック
|
286
|
+
|
287
|
+
halfAlphanumericCheck(
|
288
|
+
|
289
|
+
$_SESSION["errors"],
|
290
|
+
|
291
|
+
$user_name,
|
292
|
+
|
293
|
+
"ユーザー名は半角英数字で入力してください。"
|
294
|
+
|
295
|
+
);
|
296
|
+
|
297
|
+
halfAlphanumericCheck(
|
298
|
+
|
299
|
+
$_SESSION["errors"],
|
300
|
+
|
301
|
+
$user_password,
|
302
|
+
|
303
|
+
"パスワードは半角英数字で入力してください。"
|
304
|
+
|
305
|
+
);
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
// - メールアドレス重複チェック
|
310
|
+
|
311
|
+
mailAddressDuplicationCheck(
|
312
|
+
|
313
|
+
$_SESSION["errors"],
|
314
|
+
|
315
|
+
$user_email,
|
316
|
+
|
317
|
+
"既に登録されているメールアドレスです。"
|
318
|
+
|
319
|
+
);
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
|
43
|
-
|
325
|
+
if ($_SESSION["errors"]) {
|
326
|
+
|
327
|
+
header("Location: ../../user/");
|
328
|
+
|
329
|
+
exit();
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
// DB接続処理
|
336
|
+
|
337
|
+
$database_handler = getDatabaseConnection();
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
try {
|
342
|
+
|
343
|
+
if (
|
344
|
+
|
345
|
+
$statement = $database_handler->prepare(
|
346
|
+
|
347
|
+
"INSERT INTO users (name, email, password) VALUES (:name, :email, :password)"
|
348
|
+
|
349
|
+
)
|
350
|
+
|
351
|
+
) {
|
352
|
+
|
353
|
+
$password = password_hash($user_password, PASSWORD_DEFAULT);
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
$statement->bindParam(":name", htmlspecialchars($user_name));
|
358
|
+
|
359
|
+
$statement->bindParam(":email", $user_email);
|
360
|
+
|
361
|
+
$statement->bindParam(":password", $password);
|
362
|
+
|
363
|
+
$statement->execute();
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
} catch (Throwable $e) {
|
368
|
+
|
369
|
+
echo $e->getMessage();
|
370
|
+
|
371
|
+
exit();
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
// メモ投稿画面にリダイレクト
|
378
|
+
|
379
|
+
header("Location: ../../user/");
|
380
|
+
|
381
|
+
exit();
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
```
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
common/database.php
|
390
|
+
|
391
|
+
```php
|
44
392
|
|
45
393
|
<?php
|
46
394
|
|
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
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
395
|
+
/**
|
396
|
+
|
397
|
+
* PDOを使ってデータベースに接続する
|
398
|
+
|
399
|
+
* @return PDO
|
400
|
+
|
401
|
+
*/
|
402
|
+
|
403
|
+
function getDatabaseConnection()
|
404
|
+
|
405
|
+
{
|
406
|
+
|
407
|
+
try {
|
408
|
+
|
409
|
+
$database_handler = new PDO(
|
410
|
+
|
411
|
+
"mysql:host=db;dbname=simple_memo;charset=utf8mb4",
|
412
|
+
|
413
|
+
"root",
|
414
|
+
|
415
|
+
"password"
|
416
|
+
|
417
|
+
);
|
418
|
+
|
419
|
+
} catch (PDOException $e) {
|
420
|
+
|
421
|
+
echo "DB接続に失敗しました。<br/>";
|
422
|
+
|
423
|
+
echo $e->getMessage();
|
424
|
+
|
425
|
+
exit();
|
426
|
+
|
427
|
+
}
|
428
|
+
|
429
|
+
return $database_handler;
|
430
|
+
|
431
|
+
}
|
432
|
+
|
433
|
+
```
|
434
|
+
|
435
|
+
common/validation.php
|
436
|
+
|
437
|
+
```php
|
438
|
+
|
439
|
+
<?php
|
440
|
+
|
441
|
+
/**
|
442
|
+
|
443
|
+
* 空チェック
|
444
|
+
|
445
|
+
* @param $errors
|
446
|
+
|
447
|
+
* @param $check_value
|
448
|
+
|
449
|
+
* @param $message
|
450
|
+
|
451
|
+
*/
|
452
|
+
|
453
|
+
function emptyCheck(&$errors, $check_value, $message)
|
454
|
+
|
455
|
+
{
|
456
|
+
|
457
|
+
if (empty(trim($check_value))) {
|
458
|
+
|
459
|
+
array_push($errors, $message);
|
460
|
+
|
461
|
+
}
|
462
|
+
|
463
|
+
}
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
/**
|
468
|
+
|
469
|
+
* 最小文字数チェック
|
470
|
+
|
471
|
+
* @param $errors
|
472
|
+
|
473
|
+
* @param $check_value
|
474
|
+
|
475
|
+
* @param $message
|
476
|
+
|
477
|
+
* @param int $min_size
|
478
|
+
|
479
|
+
*/
|
480
|
+
|
481
|
+
function stringMinSizeCheck(&$errors, $check_value, $message, $min_size = 8)
|
482
|
+
|
483
|
+
{
|
484
|
+
|
485
|
+
if (mb_strlen($check_value) < $min_size) {
|
486
|
+
|
487
|
+
array_push($errors, $message);
|
488
|
+
|
489
|
+
}
|
490
|
+
|
491
|
+
}
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
/**
|
496
|
+
|
497
|
+
* 最大文字数チェック
|
498
|
+
|
499
|
+
* @param $errors
|
500
|
+
|
501
|
+
* @param $check_value
|
502
|
+
|
503
|
+
* @param $message
|
504
|
+
|
505
|
+
* @param int $max_size
|
506
|
+
|
507
|
+
*/
|
508
|
+
|
509
|
+
function stringMaxSizeCheck(&$errors, $check_value, $message, $max_size = 255)
|
510
|
+
|
511
|
+
{
|
512
|
+
|
513
|
+
if ($max_size < mb_strlen($check_value)) {
|
514
|
+
|
515
|
+
array_push($errors, $message);
|
516
|
+
|
517
|
+
}
|
518
|
+
|
519
|
+
}
|
520
|
+
|
521
|
+
|
522
|
+
|
523
|
+
/**
|
524
|
+
|
525
|
+
* メールアドレスチェック
|
526
|
+
|
527
|
+
* @param $errors
|
528
|
+
|
529
|
+
* @param $check_value
|
530
|
+
|
531
|
+
* @param $message
|
532
|
+
|
533
|
+
*/
|
534
|
+
|
535
|
+
function mailAddressCheck(&$errors, $check_value, $message)
|
536
|
+
|
537
|
+
{
|
538
|
+
|
539
|
+
if (filter_var($check_value, FILTER_VALIDATE_EMAIL) == false) {
|
540
|
+
|
541
|
+
array_push($errors, $message);
|
542
|
+
|
543
|
+
}
|
544
|
+
|
545
|
+
}
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
/**
|
550
|
+
|
551
|
+
* 半角英数字チェック
|
552
|
+
|
553
|
+
* @param $errors
|
554
|
+
|
555
|
+
* @param $check_value
|
556
|
+
|
557
|
+
* @param $message
|
558
|
+
|
559
|
+
*/
|
560
|
+
|
561
|
+
function halfAlphanumericCheck(&$errors, $check_value, $message)
|
562
|
+
|
563
|
+
{
|
564
|
+
|
565
|
+
if (preg_match("/^[a-zA-Z0-9]+$/", $check_value) == false) {
|
566
|
+
|
567
|
+
array_push($errors, $message);
|
568
|
+
|
569
|
+
}
|
570
|
+
|
571
|
+
}
|
572
|
+
|
573
|
+
|
574
|
+
|
575
|
+
/**
|
576
|
+
|
577
|
+
* メールアドレス重複チェック
|
578
|
+
|
579
|
+
* @param $errors
|
580
|
+
|
581
|
+
* @param $check_value
|
582
|
+
|
583
|
+
* @param $message
|
584
|
+
|
585
|
+
*/
|
586
|
+
|
587
|
+
function mailAddressDuplicationCheck(&$errors, $check_value, $message)
|
588
|
+
|
589
|
+
{
|
590
|
+
|
591
|
+
$database_handler = getDatabaseConnection();
|
592
|
+
|
593
|
+
if (
|
594
|
+
|
595
|
+
$statement = $database_handler->prepare(
|
596
|
+
|
597
|
+
"SELECT id FROM users WHERE email = :user_email"
|
598
|
+
|
599
|
+
)
|
600
|
+
|
601
|
+
) {
|
602
|
+
|
603
|
+
$statement->bindParam(":user_email", $check_value);
|
604
|
+
|
605
|
+
$statement->execute();
|
606
|
+
|
607
|
+
}
|
608
|
+
|
609
|
+
|
610
|
+
|
611
|
+
$result = $statement->fetch(PDO::FETCH_ASSOC);
|
612
|
+
|
613
|
+
if ($result) {
|
614
|
+
|
615
|
+
array_push($errors, $message);
|
616
|
+
|
617
|
+
}
|
618
|
+
|
619
|
+
}
|
620
|
+
|
621
|
+
|
622
|
+
|
623
|
+
```
|
624
|
+
|
625
|
+
common/header.php
|
626
|
+
|
627
|
+
```php
|
628
|
+
|
629
|
+
<?php
|
630
|
+
|
631
|
+
/**
|
632
|
+
|
633
|
+
* タイトルを指定してヘッダーを作成する
|
634
|
+
|
635
|
+
* @param $title
|
636
|
+
|
637
|
+
* @return string
|
638
|
+
|
639
|
+
*/
|
640
|
+
|
641
|
+
function getHeader($title)
|
642
|
+
|
643
|
+
{
|
644
|
+
|
645
|
+
return <<<EOF
|
646
|
+
|
647
|
+
<head>
|
648
|
+
|
649
|
+
<meta charset="utf-8"/>
|
650
|
+
|
651
|
+
<title>SimpleMemo | {$title}</title>
|
652
|
+
|
653
|
+
<link rel="stylesheet" type="text/css" href="../public/css/bootstrap.min.css" />
|
654
|
+
|
655
|
+
<link rel="stylesheet" type="text/css" href="../public/css/main.css" />
|
656
|
+
|
657
|
+
<script defer src="../public/js/all.js"></script>
|
658
|
+
|
659
|
+
</head>
|
660
|
+
|
661
|
+
EOF;
|
662
|
+
|
663
|
+
}
|
664
|
+
|
665
|
+
|
162
666
|
|
163
667
|
```
|
164
668
|
|