티스토리 뷰

다음은 회원 가입 기능을 구현하는 코드입니다.

중복된 아이디를 검사하고 입력한 비밀번호를 한번 더 입력해 동일한 비밀번호인지 확인하는 기능이 들어가 있습니다.

전체 코드는 https://github.com/dhffll/php-Mysql-Board에서 확인하실 수 있습니다.

 

resister.php (회원가입 폼 페이지)

<?php
    include 'header.php';
?>
    <h1>Resister</h1>
    <form action="resister2.php" method="POST" onsubmit="return check()">
        <p><input type="text" name="id" id="id" placeholder="id" autocomplete="off"></p>
        <a href="javascript:void(0)" id="test" onclick="test()">id 중복 확인</a>
        <div id="info"></div>
        
        <p><input type="password" name="pw" id="pw" placeholder="pw" autocomplete="off"></p>
        <p><input type="password" name="pw2" id="pw2" placeholder="pw confirm"></p>
        <p><input type="submit" value="resister"></p>
    </form>
    <a href="index.php">cancel</a>
</body>

<script src="js/jquery.min.js"></script>
<script src="js/resister.js"></script>

<?php 
    include 'footer.php';
?>

 

resister.js (아이디 중복 확인을 위한 JS 파일)

아이디 중복 검사는 Ajax로 처리해주었습니다.

function test(){
    let id = document.getElementById("id");
    let info = document.getElementById("info");
    let test = document.getElementById("test");

    $.ajax({
        type: 'POST',
        url: 'check_id.php',
        data: {
            id: id.value
        },
        success: function(data){
            if(data==0){
                info.innerText = "사용 중인 아이디입니다";
            }else{
                test.style.display="none";
                info.innerHTML = "사용 가능한 아이디입니다 <a href='resister.php'>reset</a> ";
                id.readOnly = true;
            }
        }
    })

}

function check(){

    let pw = document.getElementById("pw").value;
    let pw2 = document.getElementById("pw2").value;

    if(id.readOnly != true){
        alert("아이디 중복 체크를 해주세요.");
        return false;
    }else if(pw != pw2){
        alert("비밀번호가 일치하지 않습니다.");
        return false;
    }else{
        return true;
    }
}

 

check_id.php (Ajax 아이디 중복 확인을 위한 php 파일 - 회원 정보 테이블에 해당 아이디가 있는지 중복 검사)

<?php
    include 'mysql.php';
    
    $filtered = mysqli_real_escape_string($con,$_POST['id']);

    $sql = "
        select * from member where id = '$filtered'
    ";

    $result = mysqli_query($con,$sql);
    $row = mysqli_fetch_array($result);

    if($row['id']==$filtered){
        $send = (int)0; //같은 아이디가 있으면 0을 담아주고
    }else{
        $send = (int)1; //없으면 1을 담아주기
    }
    print($send);
?>

 

resister2.php (아이디 중복 확인 후에 ID,PW를 DB에 저장)

<?php 
    if(!empty($_POST['id'])){
        $con = mysqli_connect('localhost', 'root', 'lotion1023!', 'db00');
        $filtered=[
            'id' => mysqli_real_escape_string($con,$_POST['id']),
            'pw' => mysqli_real_escape_string($con,$_POST['pw'])
        ];
        $sql = "
            insert into member
            (id, password)
            values(
                '{$filtered['id']}',
                '{$filtered['pw']}'
            )
        ";
        $result = mysqli_query($con,$sql);

        if($result){
            echo "success!";
            echo "<a href='login.php'>로그인하기</a>";
        }else{
            echo mysqli_error($con);
        }
    }
?>

php만을 사용해 파일이 4개나 만들어졌는데요, codeigniter 프레임워크를 이용하면 간단하게 구현할 수 있습니다.

프레임워크를 사용하면 더 간단하고 짜임새있게 구현할 수 있지만, 처음 php를 접하는 초심자에게는 기본 php 개념을 먼저 익히는 것도 중요하지 않을까 생각이 드네요.

CI를 사용한 게시판은 다음 포스팅에 올려보도록 하겠습니다!