PHPほぼ忘れてしまったので勉強中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php session_start(); ini_set("display_errors", 1); ini_set("error_reporting", E_ALL); $usr = filter_input(INPUT_POST, "usr", FILTER_SANITIZE_FULL_SPECIAL_CHARS); $pwd = filter_input(INPUT_POST, "pwd", FILTER_SANITIZE_FULL_SPECIAL_CHARS); $token = filter_input(INPUT_POST, "token", FILTER_SANITIZE_FULL_SPECIAL_CHARS); $auth = [ "taro" => ["id" => "100", "pwd" => password_hash("123", PASSWORD_DEFAULT)], "jiro" => ["id" => "200", "pwd" => password_hash("234", PASSWORD_DEFAULT)], ]; if ($_SERVER["REQUEST_METHOD"] === "POST" && $token === $_SESSION["token"]) { if (isset($auth[$usr]) && password_verify($pwd, $auth[$usr]["pwd"])) { session_regenerate_id(); $_SESSOIN["id"] = $auth[$usr]["id"]; echo "OK"; } else { echo "ユーザー名またはパスワードが違います。"; } } $_SESSION["token"] = md5(mt_rand()); ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> </head> <body> <form method="post"> usr: <input type="text" name="usr"> pwd: <input type="text" name="pwd"> <input type="hidden" name="token" value="<?php echo $_SESSION["token"]; ?>"> <input type="submit"> </form> </body> </html> |