'시큐어 코딩'에 해당되는 글 1건

728x90

jQuery 와 PHP 를 조합하여 로그인 실패시 접속시도 5회가 넘으면 접속을 차단하고, 관리자한테 문의하여 비밀번호를 초기화 요청할 수도 있고, 5분(지정시간) 경과후 다시 로그인을 시도할 수 있도록 하는 코드를 구현했다.


jQuery 함수만 적어둔다.


$('#login_submit').on('click',function(e){
    e.preventDefault();
    var userID = $('#loginID');
    var userPW = $('#loginPW');

    if(userID.val() ==''){
        alert('아이디를 입력하세요');
        userID.focus();
        return false;
    }

    if(userPW.val() ==''){
        alert('비밀번호를 입력하세요');
        userPW.focus();
        return false;
    }

    if(userPW.val().search(/\s/) != -1){
        alert("비밀번호는 공백없이 입력해주세요.");
        return false;
    }

    $.ajax({
        url:loginpath+'loginChk.php',
        type: 'POST',
        data: {
            userID:$("#loginID").val(),
            password:$('#loginPW').val()
        },
        dataType:'json',
        success:function(msg){
            switch(msg.result){
                case 0:
                    alert('입력한 정보를 다시 한번 확인하세요.');
                    break;
                case 1:
                case 2:
                    alert('로그인 '+msg.result+'회 실패했습니다');
                    break;
                case 3:
                case 4:
                case 5:
                    alert('로그인 '+msg.result+'회 실패했습니다\n 5회 틀릴 경우 접속이 차단됩니다.');
                    break;
                case 10:
                    alert('관리자가 로그인을 차단했습니다.');
                    break;
                case 11:
                    location.replace('index.php'); // 화면 갱신
                    break;
                case 12:
                    alert('가입 승인대기 중입니다.');
                    break;
                case 13:
                    alert('로그인 횟수 초과로 계정이 잠겼습니다.\n5분 후에 다시 로그인 하세요.');
                    break;
                case 21:
                    location.replace('index.php'); // 화면 갱신
                    break;
                default:
                    alert('다시 한번 확인하세요\n 에러메시지 : '+msg.result);
                    break;
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert("arjax error : " + textStatus + "\n" + errorThrown);
        }
    });

});
 


PHP 핵심 함수 코드이고 나머지 함수는 직접 구현하면 된다.

function LoginSuccessChk($userID,$password){ // Web 접속
    global $db;
    if(empty($userID) || empty($password)){
        return 0;
    } else {
        if($this->isUserExisted($userID) ==1){ // 가입된 userID가 존재하면
            if($this->PasswordChk($userID,$password) == 0){ // 패스워드 불일치
                // 1. access_failed_count 가 5 이상이면 access 를 3 으로 세팅한다.
                $rs = $this->updateAccountLock($userID);
                if($rs == 1){
                    // 3. DB에 저장된 시간과 현재 접속한 시간이 5분이 경과되면 자등으로 access=1 로 설정 변경한다.
                    $gaptime = $this->TimeDiffChk($userID);
                    if($gaptime == 1){
                        $this->clearAccountLock($userID);
                        return 0;
                    } else {
                        // 4. 5분이 경과되지 않았으면 메시지를 팝업한다.
                        return 13;
                    }
                } else {
                    // 2. access_failed_count 를 1 을 증가시킨다.
                    $this->plusLoginFailCount($userID);
                    return $this->LoginFailCountChk($userID); // 로그인 실패 횟수 반환
                }
            } else { // 패스워드 일치
                $rs = $this->AccessStatusChk($userID);
                if($rs == 11) { // 로그인 허용
                    // 1. access_failed_count 를 0 으로 초기화한다.
                    $this->clearLoginFailCount($userID);
                    // 2. access 를 1로 초기화한다.
                    $this->clearAccountLock($userID);
                    // 3. 접속일자 및 시간을 수정한다.
                    $this->AccessLog($userID,'');
                    return 11;
                } else if($rs == 13){
                    // DB에 저장된 시간과 현재 접속한 시간이 5분이 경과되면 자등으로 access=1 로 설정 변경한다.
                    $gaptime = $this->TimeDiffChk($userID);
                    if($gaptime == 1) {
                        $this->clearAccountLock($userID);
                        $this->AccessLog($userID,'');
                        return 11; // 로그인 허용
                    } else {
                        return 13; // 로그인 차단
                    }
                } else {
                    return $rs; // 10:불허, 12:승인대기, 13:차단
                }
            }
        } else {
            return 0; // 가입된 userID가 없으면
        }
    }
}


블로그 이미지

Link2Me

,