ログイン機能を作成しようと思っているのですが
エラーメッセージを取得できずに苦戦しています。
empty部分をisesetなどに変えて試しましたがうまく動作しませんでした。
POSTされてきた値が空だった場合に
err_msgに格納していき
html部分でforeachで未入力の情報を書き出す。
と、php部分でerr_msgに配列が渡されていたら
if(count($err_msg) === 0){
エラーメッセージが0であればこの後にSQL実行文
}
以後の処理には行かせないようにしたいと思っています。
ですが、フォーム画面で未入力のままログインボタンを押すと
エラー表示が出ずに次ページへ遷移されてしまいます。
php
1<?php 2 session_start();//セッション開始 3 require_once "./common_function.php";//common_function呼び込み 4 //DBハンドルの取得 5 $pdo = get_dbh(); 6 $err_msg = array(); 7 8 if($_SERVER['REQUEST_METHOD'] === 'POST'){ 9 //メールアドレスとパスワードを取得 10 if(empty($_POST['account_email']) === FALSE){ 11 $account_email = $_POST['account_email']; 12 }else{ 13 $err_msg[] = 'メールアドレスが未入力です。'; 14 } 15 if(empty($_POST['account_password']) === FALSE){ 16 $account_password = $_POST['account_password']; 17 }else{ 18 $err_msg[] = 'パスワードが未入力です。'; 19 } 20 var_dump($err_msg); 21 //var_dump($account_email); 22 //var_dump($account_password); 23 if(count($err_msg) === 0){ 24 try{ 25 //sql実行 26 $sql = 'SELECT * FROM account Where account_email = :account_email and account_password = :account_password'; 27 $stmt = $pdo->prepare($sql); 28 $stmt -> bindValue(':account_email', $account_email, PDO::PARAM_STR); 29 $stmt -> bindValue(':account_password', $account_password, PDO::PARAM_STR); 30 $stmt->execute(); 31 //var_dump($stmt); 32 $count = $stmt -> rowCount(); 33 if($count > 0){ 34 header('Location: ./item_list.php'); 35 } 36 //認証処理 37 }catch(PDOException $e){ 38 echo $e->getMessage(); 39 } 40 } 41 } 42 $pdo = null; 43?> 44<!DOCTYPE html> 45<html> 46<head> 47 <title>ログイン画面</title> 48 <link rel="stylesheet" href="login.css"> 49</head> 50<body> 51 <div class="wrapper"> 52 <H1>ログイン画面</H1> 53 <?php foreach($err_msg as $value) {?> 54 <p><?php print $value ?></p> 55 <?php } ?> 56 <form action="item_list.php" method="POST"> 57 <div class="insert-form"> 58 <p class="email">メールアドレス</p> 59 <input type="email" name="account_email"> 60 <p class="password">パスワード(8文字以上、時半角英数字のみ使用可能)</p> 61 <input type="password" name="account_password"> 62 </div> 63 <input type="submit" name="login" value="ログイン"> 64 </form> 65 <input type ="button" value="新規登録" onclick="location.href='./register.php'"> 66 </div> 67</body> 68</html> 69
php2
1<?php 2 session_start();//セッション開始 3 require_once "./common_function.php";//common_function呼び込み 4 //DBハンドルの取得 5 $pdo = get_dbh(); 6 $err_msg = array(); 7 8 if($_SERVER['REQUEST_METHOD'] === 'POST'){ 9 //メールアドレスとパスワードを取得 10 if(isset($_POST['account_email']) === TRUE){ 11 $account_email = $_POST['account_email']; 12 }else{ 13 $err_msg[] = 'メールアドレスが未入力です。'; 14 } 15 if(isset($_POST['account_password']) === TRUE){ 16 $account_password = $_POST['account_password']; 17 }else{ 18 $err_msg[] = 'パスワードが未入力です。'; 19 } 20 var_dump($err_msg); 21 //var_dump($account_email); 22 //var_dump($account_password); 23 if(count($err_msg) === 0){ 24 try{ 25 //sql実行 26 $sql = 'SELECT * FROM account Where account_email = :account_email and account_password = :account_password'; 27 $stmt = $pdo->prepare($sql); 28 $stmt -> bindValue(':account_email', $account_email, PDO::PARAM_STR); 29 $stmt -> bindValue(':account_password', $account_password, PDO::PARAM_STR); 30 $stmt->execute(); 31 //var_dump($stmt); 32 $count = $stmt -> rowCount(); 33 if($count > 0){ 34 header('Location: ./item_list.php'); 35 } 36 //認証処理 37 }catch(PDOException $e){ 38 echo $e->getMessage(); 39 } 40 } 41 } 42 $pdo = null; 43?> 44<!DOCTYPE html> 45<html> 46<head> 47 <title>ログイン画面</title> 48 <link rel="stylesheet" href="login.css"> 49</head> 50<body> 51 <div class="wrapper"> 52 <H1>ログイン画面</H1> 53 <?php foreach($err_msg as $value) {?> 54 <p><?php print $value ?></p> 55 <?php } ?> 56 <form action="item_list.php" method="POST"> 57 <div class="insert-form"> 58 <p class="email">メールアドレス</p> 59 <input type="email" name="account_email"> 60 <p class="password">パスワード(8文字以上、時半角英数字のみ使用可能)</p> 61 <input type="password" name="account_password"> 62 </div> 63 <input type="submit" name="login" value="ログイン"> 64 </form> 65 <input type ="button" value="新規登録" onclick="location.href='./register.php'"> 66 </div> 67</body> 68</html> 69