###前提・実現したいこと
PHPで、とあるウェブサイトを作っています。
ユーザー登録をして、その流れで登録後にログインをする仕組みにしているのですが、ログインできません。
登録する情報はusername,email,passwordで、ログインするときはusernameとemailの入力を求めます。
参考にしたサイトはドットインストールの
です。(プレミアム会員でないと閲覧できません)
こちらでは登録・ログインともにemailとpasswordだけだったのですが、usernameを追加して、ログインもemail→usernameに変えたところエラーが発生しました。
###発生している問題・エラーメッセージ
Notice: Undefined index: username in /Applications/MAMP/~中略~/Model/User.php on line 23
###User.php
PHP
1<?php 2 3namespace MyApp\Model; 4 5class User extends \MyApp\Model { 6 7 public function create($values) { 8 $stmt = $this->db->prepare("insert into users (username, email, password, created, modified) values (:username, :email, :password, now(), now())"); 9 $res = $stmt->execute([ 10 ':username' => $values['username'], 11 ':email' => $values['email'], 12 ':password' => password_hash($values['password'], PASSWORD_DEFAULT) 13 ]); 14 15 if ($res === false) { 16 throw new \MyApp\Exception\DuplicateUsername(); 17 } 18 } 19 20 public function login($values) { 21 $stmt = $this->db->prepare("select * from users where username = :username"); 22 $stmt->execute([ 23 ':username' => $values['username'] 24 ]); 25 $stmt->setFetchMode(\PDO::FETCH_CLASS, 'stdClass'); 26 $user = $stmt->fetch(); 27 28 if (empty($user)) { 29 throw new \MyApp\Exception\UnmatchUsernameOrPassword(); 30 } 31 32 if (!password_verify($values['password'], $user->password)) { 33 throw new \MyApp\Exception\UnmatchUsernameOrPassword(); 34 } 35 36 return $user; 37 } 38 39 public function findAll() { 40 $stmt = $this->db->query("select * from users order by id"); 41 $stmt->setFetchMode(\PDO::FETCH_CLASS, 'stdClass'); 42 return $stmt->fetchAll(); 43 } 44}
###index.phpの主要部分
HTML
1<?php 2 3// ログイン 4 5require_once(__DIR__ . '/../config/config.php'); 6 7$app = new MyApp\Controller\Login(); 8 9$app->run(); 10 11// echo "login screen"; 12// exit; 13 14?> 15--- 16<form action="" method="post" id="login"> 17 <p><input type="text" name="username" placeholder="username" value="<?= isset($app->getValues()->username) ? h($app->getValues()->username) : ''; ?>"></p> 18 <p><input type="password" name="password" placeholder="password"></p> 19 <p class="err"><?= h($app->getErrors('login')); ?></p> 20 <div class="btn" onclick="document.getElementById('login').submit();">Log In</div> 21 <p class="fs12"><a href="/signup.php">Sign Up</a></p> 22 <input type="hidden" name="token" value="<?= h($_SESSION['token']); ?>"> 23 </form>
###Login.php
php
1<?php 2 3namespace MyApp\Controller; 4 5class Login extends \MyApp\Controller { 6 7 public function run() { 8 if ($this->isLoggedIn()) { 9 header('Location: ' . SITE_URL); 10 exit; 11 } 12 13 if ($_SERVER['REQUEST_METHOD'] === 'POST') { 14 $this->postProcess(); 15 } 16 } 17 18 protected function postProcess() { 19 try { 20 $this->_validate(); 21 } catch (\MyApp\Exception\EmptyPost $e) { 22 $this->setErrors('login', $e->getMessage()); 23 } 24 25 $this->setValues('username', $_POST['username']); 26 27 if ($this->hasError()) { 28 return; 29 } else { 30 try { 31 $userModel = new \MyApp\Model\User(); 32 $user = $userModel->login([ 33 'password' => $_POST['password'] 34 ]); 35 } catch (\MyApp\Exception\UnmatchUsernameOrPassword $e) { 36 $this->setErrors('login', $e->getMessage()); 37 return; 38 } 39 40 // login処理 41 session_regenerate_id(true); 42 $_SESSION['me'] = $user; 43 44 // redirect to home 45 header('Location: ' . SITE_URL); 46 exit; 47 } 48 } 49 50 private function _validate() { 51 if (!isset($_POST['token']) || $_POST['token'] !== $_SESSION['token']) { 52 echo "Invalid Token!"; 53 exit; 54 } 55 56 if (!isset($_POST['username']) || !isset($_POST['password'])) { 57 echo "Invalid Form!"; 58 exit; 59 } 60 61 if ($_POST['username'] === '' || $_POST['password'] === '') { 62 throw new \MyApp\Exception\EmptyPost(); 63 } 64 } 65 66}
宜しくお願い致します。
###補足情報(言語/FW/ツール等のバージョンなど)
PHP5.6.10
データでベースに登録されていることは確認しました。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/05 01:26