質問編集履歴

1

登録処理resister.phpのコード追加

2020/01/14 15:18

投稿

KazKit
KazKit

スコア8

test CHANGED
File without changes
test CHANGED
@@ -235,3 +235,145 @@
235
235
 
236
236
 
237
237
  どこが間違っているのでしょうか。
238
+
239
+
240
+
241
+ 追記
242
+
243
+ ```resister.php
244
+
245
+ <?php
246
+
247
+
248
+
249
+ $errorMessageEmail = '';
250
+
251
+ $errorMessagePass = '';
252
+
253
+
254
+
255
+ if (isset($_POST['submit'])) {
256
+
257
+
258
+
259
+ if ($_POST['email'] === '') {
260
+
261
+ $errorMessageEmail = ' Fill in the E-mail address';
262
+
263
+ } elseif($_POST['password'] === '') {
264
+
265
+ $errorMessagePass = ' Fill in the Password';
266
+
267
+ }
268
+
269
+
270
+
271
+ if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
272
+
273
+ $incorrectEmail = 'Your email type is not correct';
274
+
275
+ }
276
+
277
+
278
+
279
+ if (!preg_match('/\A[a-zA-Z0-9]+\z/', $_POST['password'])) {
280
+
281
+ $incorrectPass = 'Your Password type is not correct';
282
+
283
+ }
284
+
285
+
286
+
287
+ // if (!isset($_SESSION['token'])) {
288
+
289
+ // $_SESSION['token'] = bin2hex(openssl_random_pseudo_bytes(16));
290
+
291
+ // }
292
+
293
+
294
+
295
+ // if (!isset($_POST['token']) || $_POST['token'] !== $_SESSION['token']) {
296
+
297
+ // echo 'Bad token';
298
+
299
+ // exit;
300
+
301
+ // }
302
+
303
+ // else {
304
+
305
+ // header('Location: ' . $_SERVER['HTTP_HOST']);
306
+
307
+ // }
308
+
309
+
310
+
311
+ if (!empty($_POST['email']) && !empty($_POST["password"])) {
312
+
313
+ $email = $_POST['email'];
314
+
315
+ $password = $_POST['password'];
316
+
317
+
318
+
319
+ try{
320
+
321
+ //DB接続
322
+
323
+ $db = new \PDO(PDO_DSN,DB_USERNAME,DB_PASSWORD);
324
+
325
+ //エラーをスロー
326
+
327
+ $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
328
+
329
+
330
+
331
+ if (filter_var($email, FILTER_VALIDATE_EMAIL) &&
332
+
333
+ preg_match('/\A[a-zA-Z0-9]+\z/', $password)) {
334
+
335
+
336
+
337
+ $stmt = $db->prepare("INSERT INTO users (email, password, created, modified)
338
+
339
+ VALUES (:email, :password, now(), now())");
340
+
341
+
342
+
343
+ $stmt->execute([
344
+
345
+ ':email' => $email,
346
+
347
+ ':password' => password_hash($password, PASSWORD_DEFAULT)
348
+
349
+ ]);
350
+
351
+ }
352
+
353
+
354
+
355
+
356
+
357
+ } catch(\PDOException $e){
358
+
359
+ echo $e->getMessage();
360
+
361
+ exit;
362
+
363
+ }
364
+
365
+
366
+
367
+ header('Location: ' . SITE_URL . '/login.php');
368
+
369
+ exit;
370
+
371
+ }
372
+
373
+
374
+
375
+ }
376
+
377
+
378
+
379
+ ```