Hello,

kok nae-ga ha-myun an-dweneun MAGIC...🧚

웹 프로그래밍/PHP

회원가입 및 로그인 페이지 만들기 02

✿도담도담 2018. 10. 29. 19:08

이제 본격적으로 php코드를 분석하면서 공부해보자!

login.php파일 부터 열어보겠다.

.

.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
include("./dbconn.php");  // DB연결을 위한 같은 경로의 dbconn.php를 인클루드합니다.
?>
 
<html>
<head>
    <title>Login</title>
    <link href="./style.css" rel="stylesheet" type="text/css">
</head>
<body>
 
<?php if(!isset($_SESSION['ss_mb_id'])) { // 로그인 세션이 없을 경우 로그인 화면 ?>
 
<h1>로그인</h1>
 
    <form action="./login_check.php" method="post">
        <table>
            <tr>
                <th>아이디</th>
                <td><input type="text" name="mb_id"></td>
            </tr>
            <tr>
                <th>비밀번호</th>
                <td><input type="password" name="mb_password"></td>
            </tr>
            <tr>
                <td colspan="2" class="td_center">
                    <input type="submit" value="로그인"> 
                    <a href="./register.php">회원가입</a>
                </td>
            </tr>
        </table>
    </form>
 
<?php } else { // 로그인 세션이 있을 경우 로그인 완료 화면 ?>
 
<h1>로그인을 환영합니다.</h1>
 
    <?php
    $mb_id = $_SESSION['ss_mb_id'];
 
    $sql = " select * from member where mb_id = TRIM('$mb_id') ";
    $result = mysqli_query($conn$sql);
    $mb = mysqli_fetch_assoc($result);
 
    mysqli_close($conn); // 데이터베이스 접속 종료
    ?>
    <table>
        <tr>
            <th>아이디</th>
            <td><?php echo $mb['mb_id'?></td>
        </tr>
        <tr>
            <th>이름</th>
            <td><?php echo $mb['mb_name'?></td>
        </tr>
        <tr>
            <th>이메일</th>
            <td><?php echo $mb['mb_email'?></td>
        </tr>
        <tr>
            <th>성별</th>
            <td><?php echo $mb['mb_gender'?></td>
        </tr>
        <tr>
            <th>직업</th>
            <td><?php echo $mb['mb_job'?></td>
        </tr>
        <tr>
            <th>관심언어</th>
            <td><?php echo $mb['mb_language'?></td>
        </tr>
        <tr>
            <th>이메일인증일</th>
            <td><?php echo $mb['mb_email_certify'?></td>
        </tr>
        <tr>
            <th>회원가입일</th>
            <td><?php echo $mb['mb_datetime'?></td>
        </tr>
        <tr>
            <th>회원정보 수정일</th>
            <td><?php echo $mb['mb_modify_datetime'?></td>
        </tr>
        <tr>
            <td colspan="2" class="td_center">
                <a href="./register.php?mode=modify">회원정보수정</a>
                <a href="./logout.php">로그아웃</a>
            </td>
        </tr>
    </table>
 
<?php } ?>
 
</body>
</html>
cs


흐름을 보면 세션이 없을 때와 있을 때로 나뉜다.

우리가 사용하고 있는 HTTP 프로토콜의 특성으로 연결이 지속되지 않는데

이러한 프로토콜을 이용하면서 상태를 보존하고 싶은 경우 세션을 사용하게 된다.

세션은 특정 클라이언트에서 서버로 요청을 보냈을 경우 요청과 함께 클라이언트의 정보를 가지고 있다가 후에 다른 요청이 들어올 경우 가지고 있던 정보를 비교해 어떤 브라우저인지 판단한다.


쨌든 위에서 세션이 없을 경우 로그인 화면을 띄워주고

세션이 있을 경우에는 사용자의 정보를 데이터베이스에서 가져와 나타낸다.

세션의 여부는 값이 존재하면 true를 반환하는 isset()메소드를 이용해 판별해주었다.

그리고 폼 태그를 이용해 로그인 버튼을 누를시 login_check.php가 실행 되고, 회원 가입을 누를 시 register.php로 넘어간다.

세션이 있는 경우 $_SESSION[] 변수에 담긴 세션 값을 가져와 사용자의 정보를 가져오게 된다.


이 코드를 보고 예상할 수 있는 것은 아마 로그인을 했을 시 session_start() 메소드가 실행 되고

사용자의 아이디를 $_SESSION 변수에 넣어 ss_mb_id를 만들어 준 것 같다!

이후 회원가입 코드를 살펴보자 :)