現在、簡単なログイン実装を自作しています。
まずresister.phpでemailとパスワードを入力しそこでmysqlにデータが保存されます。
そして、submitを押してデータベースにデータが格納されると、login.phpに移行して先ほどのデータとlogin.phpで入力するデータが照合され、マッチすればログイン完了し、新しい画面(まだ作っていません)に飛ぶようになっています。
うまくいかない点
PHP
1<?php 2 3session_start(); 4 5$errorMessageEmail = ''; 6$errorMessagePass = ''; 7 8if (isset($_POST['submit'])) { 9 10 if ($_POST['email'] === '') { 11 $errorMessageEmail = ' Fill in the E-mail address'; 12 } elseif($_POST['password'] === '') { 13 $errorMessagePass = ' Fill in the Password'; 14 } 15 16 if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { 17 $incorrectEmail = 'Your email type is not correct'; 18 } 19 20 if (!preg_match('/\A[a-zA-Z0-9]+\z/', $_POST['password'])) { 21 $incorrectPass = 'Your Password type is not correct'; 22 } 23 24 // if (!isset($_SESSION['token'])) { 25 // $_SESSION['token'] = bin2hex(openssl_random_pseudo_bytes(16)); 26 // } 27 28 // if (!isset($_POST['token']) || $_POST['token'] !== $_SESSION['token']) { 29 // echo 'Bad token'; 30 // exit; 31 // } 32// else { 33// header('Location: ' . $_SERVER['HTTP_HOST']); 34// } 35 36if (!empty($_POST['email']) && !empty($_POST["password"])) { 37 38 $email = $_POST['email']; 39 40 try{ 41 //DB接続 42 $db = new \PDO(PDO_DSN,DB_USERNAME,DB_PASSWORD); 43 //エラーをスロー 44 $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); 45 46 if (filter_var($email, FILTER_VALIDATE_EMAIL) && 47 preg_match('/\A[a-zA-Z0-9]+\z/', $_POST['password'])) { 48 49 $stmt = $db->prepare("SELECT * from users where email = :email"); 50 51 $user = $stmt->execute([ 52 ':email' => $email 53 ]); 54 55 $user = $stmt->fetch(PDO::FETCH_ASSOC); 56 57 // var_dump($user); //Userに入っているのはpostからわたってきたデータのみ 58 // exit(); 59 60 $password = $_POST['password']; 61 62 if (isset($user)) { 63 64 if (password_verify($password, $user['password'])) { 65 66 session_regenerate_id(true); 67 68 $sql = "SELECT * FROM users"; 69 $stmt = $db->query($sql); 70 71 // var_dump($stmt); 72 // exit; 73 74 foreach ($stmt as $row) { 75 $row['email']; 76 } 77 78 var_dump($row['email']); 79 exit(); 80 81 $_SESSION['me'] = $row['email']; 82 83 // header('Location: ' . SITE_URL); 84 // exit(); 85 86 } else { 87 $unmatch = 'Email/Password do not match the datebase'; 88 } 89 90 } else { 91 echo 'User info not set'; 92 } 93 94 } 95 96 97 } catch(\PDOException $e){ 98 echo $e->getMessage(); 99 exit; 100 } 101 102} 103 104} 105
コード中盤あたりでfetchしたデータを$userに格納してvar_dumpすると、ちゃんと入力されて照合された正しいデータが入ってきていますが、その後の$stmtのvar_dumpではうまくデータが渡っておらず、困っています。
どこが間違っているのでしょうか。
追記
resister.php
1<?php 2 3$errorMessageEmail = ''; 4$errorMessagePass = ''; 5 6if (isset($_POST['submit'])) { 7 8 if ($_POST['email'] === '') { 9 $errorMessageEmail = ' Fill in the E-mail address'; 10 } elseif($_POST['password'] === '') { 11 $errorMessagePass = ' Fill in the Password'; 12 } 13 14 if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { 15 $incorrectEmail = 'Your email type is not correct'; 16 } 17 18 if (!preg_match('/\A[a-zA-Z0-9]+\z/', $_POST['password'])) { 19 $incorrectPass = 'Your Password type is not correct'; 20 } 21 22 // if (!isset($_SESSION['token'])) { 23 // $_SESSION['token'] = bin2hex(openssl_random_pseudo_bytes(16)); 24 // } 25 26 // if (!isset($_POST['token']) || $_POST['token'] !== $_SESSION['token']) { 27 // echo 'Bad token'; 28 // exit; 29 // } 30// else { 31// header('Location: ' . $_SERVER['HTTP_HOST']); 32// } 33 34if (!empty($_POST['email']) && !empty($_POST["password"])) { 35 $email = $_POST['email']; 36 $password = $_POST['password']; 37 38 try{ 39 //DB接続 40 $db = new \PDO(PDO_DSN,DB_USERNAME,DB_PASSWORD); 41 //エラーをスロー 42 $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); 43 44 if (filter_var($email, FILTER_VALIDATE_EMAIL) && 45 preg_match('/\A[a-zA-Z0-9]+\z/', $password)) { 46 47 $stmt = $db->prepare("INSERT INTO users (email, password, created, modified) 48 VALUES (:email, :password, now(), now())"); 49 50 $stmt->execute([ 51 ':email' => $email, 52 ':password' => password_hash($password, PASSWORD_DEFAULT) 53 ]); 54 } 55 56 57 } catch(\PDOException $e){ 58 echo $e->getMessage(); 59 exit; 60 } 61 62 header('Location: ' . SITE_URL . '/login.php'); 63 exit; 64} 65 66} 67
回答1件
あなたの回答
tips
プレビュー