PHP 7.4.1
関数を別途作成した、関数内でreturnすれば良いということは思いつくのですが、もっと短くできるよい方法はないでしょうか。(ない場合は「ない」という解答でも構いません。)
また、そもそも「先頭にPHPのロジックを書くのが良くない」と考える場合は、解決策の提示をお願いします。
よろしくおねがいします。
PHP
1<?php 2namespace neptune; 3use PDO; 4 5$email = $_POST['email']; 6$password = $_POST['password']; 7if(!filter_var($email, FILTER_VALIDATE_EMAIL) || !is_string($password)) { 8 return; 9} 10 11session_start(); 12 13$database = Database::newPDO(); 14$statement = $database->prepare('SELECT salt FROM users WHERE email = ?'); 15$statement->bindValue(1, $email); 16$salt = $statement->fetch(); 17$statement = $database->prepare('SELECT id FROM users WHERE email = ? and password = ?'); 18$statement->bindValue(1, $email); 19$statement->bindValue(2, password_hash($password, $salt)); 20$_SESSION['id'] = $statement->fetch(); 21 22?> 23<!doctype html> 24<html> 25<head> 26 <meta charset="UTF-8"> 27 <meta name="viewport" 28 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 29 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 30 <title>Document</title> 31 <link rel="stylesheet" type="text/css" href="common.css"> 32 <style type="text/css"> 33 #container { 34 margin-left: auto; 35 margin-right: auto; 36 margin-top: 300px; 37 width: 350px; 38 height: 250px; 39 background-color: #555555; 40 } 41 42 #msg { 43 text-align: center; 44 padding: 20px; 45 } 46 47 #container form > *{ 48 margin-left: auto; 49 margin-right: auto; 50 margin-bottom: 10px; 51 display: block; 52 width: 220px; 53 height: 40px; 54 border: 0; 55 } 56 </style> 57</head> 58<body> 59<div id="container"> 60 <p id="msg">ログインする</p> 61 <form method="post" action="login.php" target="da"> 62 <input type="email" name="email" placeholder="メールアドレス" required> 63 <input type="password" name="password" placeholder="パスワード" required> 64 <input type="submit"> 65 </form> 66</div> 67</body> 68</html>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。