⯌ 로그인 로직 4가지
먼저, php파일의 윗부분을 먼저 보여드리겠습니다.
⟡ 식별/인증 동시 처리
<?php
$id = $_POST['id'];
$pw = $_POST['pw'];
$db_conn = mysqli_connect('localhost', 'admin', 'student1234', 'sign_up');
$sql = "select * from user_info where id='$id' and pw='$pw'";
$result = mysqli_query($db_conn, $sql);
$row = mysqli_fetch_array($result);
if ($row) { // ID와 PW가 일치할 시
echo '<div class="alert alert-success text-center" role="alert">
<strong>로그인 성공!</strong>
</div>';
$_SESSION["id"] = $id;
$_SESSION["pw"] = $pw;
} else { //ID 또는 PW 불일치 시
echo '<div class="alert alert-danger text-center" role="alert">
<strong>아이디 또는 비밀번호를 찾을 수 없습니다.</strong>
</div>';
}
mysqli_close($db_conn);
// DB 연결 종료
?>
• SQL 구문에 "where id='$id' and pw='$pw' 라는 조건을 적어 ID와 PW를 동시에 비교합니다.
⟡ 식별/인증 분리 처리
<?php
$id = $_POST['id'];
$pw = $_POST['pw'];
$db_conn = mysqli_connect('localhost', 'admin', 'student1234', 'sign_up');
$sql = "select * from user_info where id='$id'";
$result = mysqli_query($db_conn, $sql);
$row = mysqli_fetch_array($result);
if ($pw === $row['pw']) { // ID와 PW가 일치할 시
echo '<div class="alert alert-success text-center" role="alert">
<strong>로그인 성공!</strong>
</div>';
$_SESSION["id"] = $id;
$_SESSION["pw"] = $pw;
} else { //ID 또는 PW 불일치 시
echo '<div class="alert alert-danger text-center" role="alert">
<strong>아이디 또는 비밀번호를 찾을 수 없습니다.</strong>
</div>';
}
mysqli_close($db_conn);
// DB 연결 종료
?>
• SQL 구문에 "where id='$id'를 적어 식별정보를 먼저 구분한 후 인증정보를 비교합니다.
⟡ 식별/인증(Hash) 동시 처리
<?php
$id = $_POST['id'];
$pw = $_POST['pw'];
$hash_pw = hash('sha256', $pw); // 비밀번호 해시
$db_conn = mysqli_connect('localhost', 'admin', 'student1234', 'sign_up');
$sql = "select * from user_info where id='$id' and pw='$hash_pw'";
$result = mysqli_query($db_conn, $sql);
$row = mysqli_fetch_array($result);
if ($row) { // ID와 PW가 일치할 시
echo '<div class="alert alert-success text-center" role="alert">
<strong>로그인 성공!</strong>
</div>';
$_SESSION["id"] = $id;
$_SESSION["pw"] = $hash_pw;
} else { //ID 또는 PW 불일치 시
echo '<div class="alert alert-danger text-center" role="alert">
<strong>아이디 또는 비밀번호를 찾을 수 없습니다.</strong>
</div>';
}
mysqli_close($db_conn);
// DB 연결 종료
?>
• hash() 함수를 사용하여 SHA-256 해싱 알고리즘으로 해시화합니다.
• where id='$id' and pw='$hash_pw' 라는 조건을 이용하여 ID와 PW를 동시에 비교합니다.
⟡ 식별/인증(Hash) 분리 처리
<?php
$id = $_POST['id'];
$pw = $_POST['pw'];
$hash_pw = hash('sha256', $pw); // 비밀번호 해시
$db_conn = mysqli_connect('localhost', 'admin', 'student1234', 'sign_up');
$sql = "select * from user_info where id='$id'";
$result = mysqli_query($db_conn, $sql);
$row = mysqli_fetch_array($result);
if ($hash_pw === $row['pw']) { // ID와 PW가 일치할 시
echo '<div class="alert alert-success text-center" role="alert">
<strong>로그인 성공!</strong>
</div>';
$_SESSION["id"] = $id;
$_SESSION["pw"] = $hash_pw;
} else { //ID 또는 PW 불일치 시
echo '<div class="alert alert-danger text-center" role="alert">
<strong>아이디 또는 비밀번호를 찾을 수 없습니다.</strong>
</div>';
}
mysqli_close($db_conn);
// DB 연결 종료
?>
• SQL 구문에 "where id='$id'를 적어 식별정보를 먼저 구분한 후 해시화 된 인증정보를 비교합니다.
'웹 모의해킹 취업반 스터디 > 과제' 카테고리의 다른 글
[ 2주차 과제 ] 회원가입 & 로그인 DB 연동 (0) | 2025.04.16 |
---|---|
[ 1주차 과제 ] 로그인 페이지 구현 & CSS (0) | 2025.04.09 |