로그인 로직 4가지 (동시, 분리 처리) [ 3주차 ]

2025. 4. 23. 14:47·웹 모의해킹 프로젝트 - 과제/웹 이해 & 로그인 구현

1. 로그인 로직 4가지

먼저, php파일의 윗부분을 먼저 보여드리겠습니다.

1-1) 식별/인증 동시 처리

<?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를 동시에 비교합니다.

 

1-2) 식별/인증 분리 처리

<?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'를 적어 식별정보를 먼저 구분한 후 인증정보를 비교합니다.

 

1-3) 식별/인증(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를 동시에 비교합니다.

 

1-4) 식별/인증(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'를 적어 식별정보를 먼저 구분한 후 해시화 된 인증정보를 비교합니다.

'웹 모의해킹 프로젝트 - 과제 > 웹 이해 & 로그인 구현' 카테고리의 다른 글

회원가입 & 로그인 DB 연동 [ 2주차 ]  (0) 2025.04.16
로그인 페이지 구현 & CSS [ 1주차 ]  (0) 2025.04.09
'웹 모의해킹 프로젝트 - 과제/웹 이해 & 로그인 구현' 카테고리의 다른 글
  • 회원가입 & 로그인 DB 연동 [ 2주차 ]
  • 로그인 페이지 구현 & CSS [ 1주차 ]
ㅤ민도화ㅤ
ㅤ민도화ㅤ
  • ㅤ민도화ㅤ
    Security Note
    ㅤ민도화ㅤ
    • 분류 전체보기 (40)
      • 웹 모의해킹 프로젝트 - 수업 (17)
        • 웹 이해 & 로그인 구현 (4)
        • SQL 인젝션 (5)
        • XSS (3)
        • CSRF (2)
        • 파일 업로드 & 다운로드 (2)
        • 인증 & 인가 (1)
      • 웹 모의해킹 프로젝트 - 과제 (23)
        • 웹 이해 & 로그인 구현 (3)
        • SQL 인젝션 (3)
        • XSS (9)
        • CSRF (3)
        • 파일 업로드 & 다운로드 (4)
        • 인증 & 인가 (1)
  • 태그

    CSRF
    웹 모의해킹 프로젝트
    XSS
    파일 업로드 & 다운로드
    SQLi
    인증 & 인가
  • hELLO· Designed By정상우.v4.10.3
ㅤ민도화ㅤ
로그인 로직 4가지 (동시, 분리 처리) [ 3주차 ]
상단으로

티스토리툴바