로그인 방법 검증중에 식별 / 인증 분리 방식으로 로그인을 구현해 보았다.
식별/인증 분리방식이란 sql 조건문 where 에 아이디와 패스워드를 한번에 넣지않고,
아이디로 패스워드 값을 select 한 후 유저가 입력한 패스워드값과 디비값을 분리하여 인증하는 방식이다.
<?php
# 식별 인증 분리 방식 로그인
session_start();
$userId = trim($_POST['userId']);
$userPw= trim($_POST['userPw']);
$mysqli = mysqli_connect("127.0.0.1","디비유저","디비번호","데이터베이스명","3306");
if ($mysqli->connect_errno) {
die('Connect Error: '.$mysqli->connect_error);
}
$mSql = "select pw from member where id = '".$userId."';";
$result = mysqli_query( $mysqli , $mSql );
while ($rows = mysqli_fetch_array($result)) {
// code...
$existPw = $rows['pw'];
}
if (!is_null($existPw)){
#아이디가 존재
if($userPw !== $existPw){
echo "<script>alert('비밀번호가 올바르지 않습니다.'); history.back();</script>";
session_destroy();
exit;
}
}else{
#아이디 없음
echo "<script>alert('존재하지 않는 아이디 입니다.'); history.back();</script>";
session_destroy();
exit;
}
$_SESSION['userId'] = $userId;
?>
<meta http-equiv='refresh' content='1;url=/Main.php'>
위에 보면 $mSql = "select pw from member where id = '".$userId."';"; 로 입력한 아이디 값으로 패스워드 값을 먼저 조회해 온다.
if (!is_null($existPw)){
#아이디가 존재
if($userPw !== $existPw){
echo "<script>alert('비밀번호가 올바르지 않습니다.'); history.back();</script>";
session_destroy();
exit;
}
}else{
#아이디 없음
echo "<script>alert('존재하지 않는 아이디 입니다.'); history.back();</script>";
session_destroy();
exit;
}
이후 인증과정 아이디존재 유무 먼저 확인하고, 있는 아이디라면 유저가 입력한 값과 비교한다.
'Project > APM웹서버' 카테고리의 다른 글
sha256 해쉬로 로그인 구현하기 (0) | 2021.10.28 |
---|---|
로그인 인증 비밀번호 해시 ( password_verify ) (0) | 2021.10.27 |
패스워드 해쉬처리( password_hash ) (0) | 2021.10.26 |
script 에서 클릭 이벤트로 php의 메서드 실행하기 (0) | 2021.10.23 |
PHP Mysql연동을 위한 태그(mysqli_connect) (0) | 2021.10.18 |