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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
<?php Class User{ static public $salt = "mwefCMEP28DjwdW3lwdS239vVS"; } class Db{ public static $d; static public function con(){ $passwd = md5("1234" . User::$salt); try{ self::$d = new PDO('sqlite::memory:',null,null); self::$d->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); self::$d->exec("create table if not exists tbl(id, user, password)"); self::$d->exec("insert into tbl (id,user,password) values (1,'taro','$passwd')"); }catch(PDOException $e){ self::$d = null; exit($e->getMessage()); } } } class Main{ function __construct(){ Db::con(); if(isset($_POST["user"]) && isset($_POST["password"])){ $passwd = md5($_POST["password"] . User::$salt); $stmt = Db::$d->prepare("select * from tbl where password = ?"); // ここで where password = '?' としていたらエラー。 $stmt->execute(array($passwd)); $res = $stmt->fetchAll(PDO::FETCH_BOTH); if(isset($res[0]["password"])){ echo "ログイン中", $res[0]["user"], "さん"; }else{ sleep(2); echo "ログイン失敗"; echo '<form method="POST" action="">'; echo ' <input type="text" name="user">'; echo ' <input type="password" name="password">'; echo ' <input type="submit">'; echo '</form>'; } } else { echo '<form method="POST" action="">'; echo ' <input type="text" name="user">'; echo ' <input type="password" name="password">'; echo ' <input type="submit">'; echo '</form>'; } } } new Main(); |