回答編集履歴

2

修正

2017/03/01 14:19

投稿

退会済みユーザー
test CHANGED
@@ -68,6 +68,8 @@
68
68
 
69
69
 
70
70
 
71
+ -- password = 'password'
72
+
71
73
  INSERT INTO `Users` (`id`, `email`, `password`)
72
74
 
73
75
  VALUES
@@ -104,7 +106,7 @@
104
106
 
105
107
  define('USERNAME', 'root');
106
108
 
107
- define('PASSWORD', '3m3tssrr');
109
+ define('PASSWORD', 'password');
108
110
 
109
111
 
110
112
 

1

追記

2017/03/01 14:19

投稿

退会済みユーザー
test CHANGED
@@ -37,3 +37,459 @@
37
37
  require('dbconnect.php');
38
38
 
39
39
  ```
40
+
41
+
42
+
43
+ ---
44
+
45
+
46
+
47
+ #やっつけサンプル
48
+
49
+
50
+
51
+ ##SQL
52
+
53
+
54
+
55
+ ```sql
56
+
57
+ CREATE TABLE `Users` (
58
+
59
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
60
+
61
+ `email` varchar(64) DEFAULT NULL,
62
+
63
+ `password` varchar(256) DEFAULT NULL,
64
+
65
+ PRIMARY KEY (`id`)
66
+
67
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
68
+
69
+
70
+
71
+ INSERT INTO `Users` (`id`, `email`, `password`)
72
+
73
+ VALUES
74
+
75
+ (1,'user@example.com','$2y$10$OqyHPnhhs7nFZGrB/P7wDuN1sZ7olqyc8r3bUTXIKRqg00eJRu/W2');
76
+
77
+ ```
78
+
79
+ ##index.php
80
+
81
+
82
+
83
+ ```php
84
+
85
+ <?php
86
+
87
+ /**
88
+
89
+ * index.php
90
+
91
+ */
92
+
93
+ ini_set('display_errors', true);
94
+
95
+ error_reporting(E_ALL);
96
+
97
+
98
+
99
+ session_start();
100
+
101
+
102
+
103
+ define('DSN', 'mysql:host=localhost;dbname=sample;charset=utf8');
104
+
105
+ define('USERNAME', 'root');
106
+
107
+ define('PASSWORD', '3m3tssrr');
108
+
109
+
110
+
111
+ /**
112
+
113
+ * htmlspecialchars
114
+
115
+ */
116
+
117
+ function h($string)
118
+
119
+ {
120
+
121
+ return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
122
+
123
+ }
124
+
125
+
126
+
127
+ /**
128
+
129
+ * SELECT
130
+
131
+ */
132
+
133
+ function select($sql, $params = [])
134
+
135
+ {
136
+
137
+ $options = [
138
+
139
+ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
140
+
141
+ ];
142
+
143
+ $objPdo = new PDO(DSN, USERNAME, PASSWORD, $options);
144
+
145
+ $stmt = $objPdo->prepare($sql);
146
+
147
+ $stmt->execute($params);
148
+
149
+ return $stmt->fetchAll();
150
+
151
+ }
152
+
153
+
154
+
155
+ /**
156
+
157
+ * ログイン
158
+
159
+ */
160
+
161
+ function login()
162
+
163
+ {
164
+
165
+ if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') {
166
+
167
+ return;
168
+
169
+ }
170
+
171
+ $email = filter_input(INPUT_POST, 'email');
172
+
173
+ $password = filter_input(INPUT_POST, 'password');
174
+
175
+ if (empty($email) || empty($password)) {
176
+
177
+ throw new Exception('メールアドレスおよびパスワードは入力必須です。');
178
+
179
+ }
180
+
181
+
182
+
183
+ $sql = 'SELECT * FROM `Users` WHERE `email` = :email ';
184
+
185
+ $params = [];
186
+
187
+ $params['email'] = $email;
188
+
189
+ $rows = select($sql, $params);
190
+
191
+
192
+
193
+ $user = reset($rows);
194
+
195
+ if (count($rows) === 0 || !password_verify($password, $user['password'])) {
196
+
197
+ throw new Exception('メールアドレスおよびパスワードが間違っています。');
198
+
199
+ }
200
+
201
+
202
+
203
+ session_regenerate_id(true);
204
+
205
+ $_SESSION['login_user'] = $user;
206
+
207
+
208
+
209
+ return true;
210
+
211
+ }
212
+
213
+
214
+
215
+ try {
216
+
217
+ if (login()) {
218
+
219
+ header('Location: top.php');
220
+
221
+ }
222
+
223
+ } catch (Exception $ex) {
224
+
225
+ $err = $ex->getMessage();
226
+
227
+ }
228
+
229
+ ?>
230
+
231
+ <!DOCTYPE html>
232
+
233
+ <html lang="en">
234
+
235
+ <head>
236
+
237
+ <meta charset="utf-8">
238
+
239
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
240
+
241
+ <meta name="viewport" content="width=device-width, initial-scale=1">
242
+
243
+
244
+
245
+ <title>Signin Template for Bootstrap</title>
246
+
247
+
248
+
249
+ <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
250
+
251
+ <style type="text/css">
252
+
253
+ body {
254
+
255
+ padding-top: 40px;
256
+
257
+ padding-bottom: 40px;
258
+
259
+ background-color: #eee;
260
+
261
+ }
262
+
263
+
264
+
265
+ .form-signin {
266
+
267
+ max-width: 330px;
268
+
269
+ padding: 15px;
270
+
271
+ margin: 0 auto;
272
+
273
+ }
274
+
275
+ .form-signin .form-signin-heading,
276
+
277
+ .form-signin .checkbox {
278
+
279
+ margin-bottom: 10px;
280
+
281
+ }
282
+
283
+ .form-signin .checkbox {
284
+
285
+ font-weight: normal;
286
+
287
+ }
288
+
289
+ .form-signin .form-control {
290
+
291
+ position: relative;
292
+
293
+ height: auto;
294
+
295
+ -webkit-box-sizing: border-box;
296
+
297
+ -moz-box-sizing: border-box;
298
+
299
+ box-sizing: border-box;
300
+
301
+ padding: 10px;
302
+
303
+ font-size: 16px;
304
+
305
+ }
306
+
307
+ .form-signin .form-control:focus {
308
+
309
+ z-index: 2;
310
+
311
+ }
312
+
313
+ .form-signin input[type="email"] {
314
+
315
+ margin-bottom: -1px;
316
+
317
+ border-bottom-right-radius: 0;
318
+
319
+ border-bottom-left-radius: 0;
320
+
321
+ }
322
+
323
+ .form-signin input[type="password"] {
324
+
325
+ margin-bottom: 10px;
326
+
327
+ border-top-left-radius: 0;
328
+
329
+ border-top-right-radius: 0;
330
+
331
+ }
332
+
333
+ </style>
334
+
335
+ </head>
336
+
337
+
338
+
339
+ <body>
340
+
341
+
342
+
343
+ <div class="container">
344
+
345
+
346
+
347
+ <?php if (isset($err)) : ?>
348
+
349
+ <div class="alert alert-danger">
350
+
351
+ <p>
352
+
353
+ <?= h($err); ?>
354
+
355
+ </p>
356
+
357
+ </div>
358
+
359
+ <?php endif; ?>
360
+
361
+
362
+
363
+ <form class="form-signin" method="post">
364
+
365
+ <h2 class="form-signin-heading">Please sign in</h2>
366
+
367
+
368
+
369
+ <label for="inputEmail" class="sr-only">
370
+
371
+ Email address
372
+
373
+ </label>
374
+
375
+ <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" autofocus>
376
+
377
+
378
+
379
+ <label for="inputPassword" class="sr-only">
380
+
381
+ Password
382
+
383
+ </label>
384
+
385
+ <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password">
386
+
387
+
388
+
389
+ <button class="btn btn-lg btn-primary btn-block" type="submit">
390
+
391
+ Sign in
392
+
393
+ </button>
394
+
395
+ </form>
396
+
397
+
398
+
399
+ </div> <!-- /container -->
400
+
401
+ </body>
402
+
403
+ </html>
404
+
405
+ ```
406
+
407
+
408
+
409
+ ##top.php
410
+
411
+
412
+
413
+ ```php
414
+
415
+ <?php
416
+
417
+ /**
418
+
419
+ * top.php
420
+
421
+ */
422
+
423
+ ini_set('display_errors', true);
424
+
425
+ error_reporting(E_ALL);
426
+
427
+
428
+
429
+ session_start();
430
+
431
+
432
+
433
+ /**
434
+
435
+ * htmlspecialchars
436
+
437
+ */
438
+
439
+ function h($string)
440
+
441
+ {
442
+
443
+ return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
444
+
445
+ }
446
+
447
+
448
+
449
+ /**
450
+
451
+ * check_login
452
+
453
+ */
454
+
455
+ function check_login()
456
+
457
+ {
458
+
459
+ $res = (isset($_SESSION['login_user']) && $_SESSION['login_user'] != NULL);
460
+
461
+ if (!$res) {
462
+
463
+ header('Location: index.php');
464
+
465
+ }
466
+
467
+ }
468
+
469
+
470
+
471
+ check_login();
472
+
473
+ ?>
474
+
475
+ <!DOCTYPE HTML>
476
+
477
+ <html lang="ja">
478
+
479
+ <head>
480
+
481
+ <meta charset="UTF-8">
482
+
483
+ <title></title>
484
+
485
+ </head>
486
+
487
+ <body>
488
+
489
+ <?= h($_SESSION['login_user']['email']); ?>
490
+
491
+ </body>
492
+
493
+ </html>
494
+
495
+ ```