본문 바로가기

프론트앤드 수업

[mysql,php] 간단한 로그인,로그아웃 만들어보기

728x90

파일은 전에썻던 book를 그대로 사용했다.

1. members테이블 생성

create table members(
no int primary key auto_increment,
id varchar(16) not null,
pw varchar(20) not null,
date datetime not null,
name varchar(20) not null
);

2. member 폴더 생성 후 파일생성
-> join.php 회원가입
-> join_process.php 회원가입 처리
-> login.php 로그인
-> login_process.php 로그인 처리

1. 회원가입창을 만들고 post전송으로 자료를 받아온다.

2. sql문을 사용해 mysql members테이블에 값을 넣어준다.

3. 로그인창을 만들고 session을 사용해 값을 받아본다.

4. 받아온값들의 id와 pw를 비교해 맞다면 로그인이 로그아웃으로 변경되게 한다.

5. 로그아웃시 모든 세션을 지우고 로그인으로 변경해준다.

   <div id="write_book" class="inner">
                <h2>회원가입</h2>   
            <h3>회원 정보를 입력하세요</h3>
            <form action="../process/join_process.php" method="post">
                <table>
                    <tr>
                        <td>아이디</td>
                        <td><input type="text" name="userId"></td>
                    </tr>
                    <tr>
                        <td>비밀번호</td>
                        <td><input type="password" name="userPw"></td>
                    </tr>
                    <tr>
                        <td>비밀번호체크</td>
                        <td><input type="password" name="userPwCh"></td>
                    </tr>
                    <tr>
                        <td>이름</td>
                        <td><input type="text" name="userName"></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                        <button type="submit">회원가입</button>
                        <button type="reset">취소</button></td>
                    </tr>

                </table>
            </form>
    </div>
<?php
    회원가입 쿼리
    $conn = mysqli_connect('localhost','root','1234','test');
    $query = "insert into members (id,pw,date,name)
        values('{$_POST['userId']}', '{$_POST['userPw']}', NOW(), '{$_POST['userName']}')";
    $result = mysqli_query($conn,$query);
        header('Location:../index.php')
?>
    <div id="write_book" class="inner">
                <h2>로그인</h2>   
            <h3>ID와 PW를 입력하세요</h3>
            <form action="../process/login_process.php" method="post">
                <table>
                    <tr>
                        <td>아이디</td>
                        <td><input type="text" name="userId"></td>
                    </tr>
                    <tr>
                        <td>비밀번호</td>
                        <td><input type="password" name="userPw"></td>
                    <tr>
                        <td colspan="2">
                        <button type="submit">로그인</button>
                        <button type="button" onclick="location.href='join.php'">회원가입</button>
                        <button type="reset">취소</button>
                    </td>
                    </tr>

                </table>
            </form>
    </div>

button에 onclick="location.href=위치" 로 바로 이동을 시킬 수 있다. 스크립트라는데 왜 그냥되는거지 ?

<?php
    session_start();
    // member테이블에 등록된 회원인지 확인
    $conn = mysqli_connect('localhost','root','1234','test');
    $query = "select * from members where id ='{$_POST['userId']}'";
    $result = mysqli_query($conn,$query);
    // 아이디가 있다면 비밀번호 검사
    if(mysqli_num_rows($result)==1){
        // 받아온 값들을 php에서 사용가능한 배열로 변경
        $row = mysqli_fetch_array($result);
        //비밀번호 확인 -> 비밀번호 일치시 session 생성
        if($_POST['userPw'] == $row['pw']){
            $_SESSION['userId'] = $_POST['userId'];
            // 세션 아이디가 있으면 로그인 되었습니다. 경고창 출력
            if(isset($_SESSION['userId'])){?>
            <script>
                alert('로그인 되었습니다.');
                location.replace('../index.php');
            </script>
                <?php
            }  
        }else{?>
            <script>
                alert('아이디나 비밀번호가 맞지 않습니다.');
                location.replace('../index.php');
            </script> 
        <?php
        }
    }else{
        ?>
            <script>
                alert('아이디나 비밀번호가 맞지 않습니다.');
                location.replace('../index.php');
            </script>
        <?php
    }

?>

로그인 코드.

값을 모두 받아오고 ..어 여기선 안됏지만 원레 ID도 중복불가로 해놓고 입력된 ID와 동일한 ID가 1줄 있는지 확인 후 PW를 확인한다. PW가 맞다면 세션userId에 post로 받아온값을 넣어주고 세션아이디가 비어있는지 확인 후 로그인 되었다는 경고창을 띄워준다. 나머지인 경우 아이디나 비밀번호가 맞지않는걸로 ..

<?php
    session_start();
    $result = session_destroy();
    if($result){
        ?>
    <script>
        alert('로그아웃 되었습니다.')
        history.back();
        //이전페이지로 이동
    </script>
        <?php
    }
    ?>

로그아웃이 되면 다시 원레페이지로 돌려줘야하는데 ... 스크립트에서 history.back(); 를 입력하면 자동으로 이전페이지로 넘어간다. 좋은기능이야 ...

728x90
댓글