$user = 'ms2sato'; $password = 'mypassword'; $salt = createSalt(); //長い文字列 $encrypted_password = crypt($password, $salt); // 暗号化 $insertSql = "insert into user(user_id, encrypted_password, salt) values (?, ?, ?)" $stmt = $dbh->prepare($insertSql); //PDO利用していると思ってください。 $stmt->execute(array($user,$encrypted_password,$salt));
$user = 'ms2sato'; //WEBからPOSTされてきたという想定 $password = 'mypassword'; //WEBからPOSTされてきたという想定 $selectSql = "select * from user where user_id = ?"; $stmt = $dbh->prepare($selectSql); $stmt->execute(array($user)); while ($data = $stmt->fetch(PDO::FETCH_ASSOC)){ $salt = $data['salt']; $encrypted_password = crypt($password, $salt); //保存時と同じ暗号化 // パスワードの一致を調べる if($data['encrypted_password'] == $encrypted_password){ echo 'ようこそ!!'; } else { echo 'ID・パスワードが不正です'; } }