본문 바로가기

프로그래밍/웹

PHP 로그인 구현

반응형

ி 로그인 폼



자바스크립트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<script>
 
 
function IDFocus()
{
    document.getElementById("bt_user_id").focus();
}
 
function logchk_submit()
{
    var id = document.getElementById("bt_user_id").value;
    var pw = document.getElementById("bt_user_pw").value;
 
    if (id == '' || pw == '')
    {
      alert('아이디 또는 패스워드가 입력되지 않았습니다.');
    } else { 
         document.getElementById("login_form").submit();
    }
 
}
 
</script>
cs

HTML

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
<html>
 
<body onload="IDFocus()">
<center>
<div border="1" style="margin-top:50px;boder:1px solid silver;width:950px;height:600px;background-color:#f5f5f5;">
<div style="margin-top:200px;boder:1px solid silver;">
 
</div>
 
<div>
<form id="login_form" method="post" action="/login_check.php">
 
<TABLE style="VERTICAL-ALIGN: top" border=0 cellSpacing=0 cellPadding=0 width=482>
 
<TR>
<TD style="VERTICAL-ALIGN: top" height=68>
<TABLE style="VERTICAL-ALIGN: top" border=0 cellSpacing=0 cellPadding=0 width=482 height=60>
 
 
<TR>
<TD class=text02 height=30 width=120 align=right><span style="font-size:11pt;font-weight:bold;">사용자ID&nbsp;&nbsp;</span></TD>
<TD height=30><INPUT id="bt_user_id" style="WIDTH: 216px; HEIGHT: 25px" class=text02 tabIndex=1 maxLength=80 type=text name=ID>
</TD></TR>
 
<TR>
<TD class=text02 height=30 width=120 align=right><span style="font-size:11pt;font-weight:bold;">비밀번호&nbsp;&nbsp;</span></TD>
<TD height=30><INPUT id="bt_user_pw" style="WIDTH: 216px; HEIGHT: 25px" class=text02 tabIndex=2 value="" maxLength=40 type=password 
name=PWD> </TD></TR>
 
<TR>
<TD colspan="2" style="padding-left:235px;" height=30><INPUT style="WIDTH: 100px; HEIGHT: 25px" type="button" value="로그인" 
onclick="logchk_submit()"></TD></TR>
 
</TABLE>
 
</form>
</div>
 
</div>
</center>
</body>
</html>
cs



ி 로그인 처리(login_check.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
<?php
 
    // DB 연결
    require_once("db_conn.php");
    
    session_start();
    $id = $_POST['ID'];
    $pw = $_POST['PWD'];
    
    // 로그인 정보를 가져온다.
    $login_sql = "SELECT PW FROM user WHERE id='$id'";
    $result = mysqli_query($dbconn,$login_sql);
    $select_pw = mysqli_fetch_array($result);
    
    // 로그인하고 세션을 만든다.
    if ( $select_pw['PW'== $pw )
    {    
        // 세션 지속 시간 = 3시간
        $_SESSION['userid'= $id;
        if ( isset($_SESSION['userid']))
        {
           echo "<script>location.href='/main.php'</script>";
        } else { echo "세션이 만료되었습니다."; }
 
    } else {
        echo "<script>alert('아이디 또는 비밀번호가 다릅니다.');";
        echo "location.href='/login.php'</script>";
    }
 
?>
cs



ி 로그아웃

1
2
3
4
5
6
7
8
9
10
11
12
<?php
 
// 세션 삭제
session_start();
session_unset(); 
$res = session_destroy();
 
if ($res){ // 로그아웃되면 로그인 페이지로 이동
echo "<script> alert('로그아웃 되었습니다.'); location.href='/login.php'</script>";
}
 
?>
cs



데이터 연동

https://jdh5202.tistory.com/525

PHP 함수 모음

https://jdh5202.tistory.com/523


반응형