본문 바로가기
프로젝트/게시판 사이트

[html, php] 게시판사이트 만들기 3.로그인/회원가입

by 바디스 2021. 3. 1.

로그인

데이터베이스에서 회원정보를 읽어 아이디와 패스워드를 비교하여 로그인 하는 페이지

<login.php> - 로그인 ui

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
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>K-borad 로그인</title>
    <style>
      * {margin: 0; padding: 0;}
      #login_box{
        width:400px;
        height:150px;
        border:solid 2px gray;
        position: absolute;
        left: 50%; top: 50%;
        margin-left: -200px;
        margin-top: -100px;
        background-color: orange;
      }
      .submit{
        width: 80px;
        height: 70px;
        border-radius: 5px;
        position: absolute;
        left: 50%; top: 50%;
        margin-left: 80px;
        margin-top: -60px;
      }
      a{
        color: black;
        text-decoration: none;
      }
    </style>
  </head>
  <body>
    <div id="login_box">
      <form name="loginForm" action="login_search.php" method="post">
        <table width="300" height="100" border="0">
          <tr>
            <th align="right">아 이 디 :</th>
            <td><input type="text" name="userid"></td>
          </tr>
          <tr>
            <th align="right">패스워드 :</th>
            <td><input type="password" name="userpw"></td>
          </tr>
        </table>
        <input type="submit" class="submit" value="로그인">
        <p align=center>
        <a href="sign_up.php">회원가입</a> |
        <a href="#">비밀번호 찾기</a>
        </p>
      </form>
 
    </div>
  </body>
</html>
 
cs

 

<login_search.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
<?
 
  $connect = mysql_connect("localhost","kdhong","1234");
  mysql_set_charset("utf8",$connect);
  mysql_select_db("kdhong_db",$connect);
 
  $id = $_POST['userid'];
  $pw = $_POST['userpw'];
 
  if (!$id) {
    echo "
    <script>
      window.alert('아이디를 입력하세요');
      history.go(-1);
      </script>";
  }
  elseif (!$pw) {
    echo "
    <script>
      window.alert('패스워드를 입력하세요')
      history.go(-1)
      </script>";
 
  }
  else {
 
 
  $sql = "select * from user where id='$id'";
  $result = mysql_query($sql,$connect);
  $num1 = mysql_num_rows($result);
 
  $sql = "select * from user where id='$id' and pw='$pw'";
  $result = mysql_query($sql,$connect);
  $num2 = mysql_num_rows($result);
  if (!$num1) {
    echo "
    <script>
      window.alert('아이디/비밀번호가 틀렸습니다 다시 입력하세요')
      history.go(-1)
      </script>";
  }
  elseif (!$num2) {
    echo "
    <script>
      window.alert('아이디/비밀번호가 틀렸습니다 다시 입력하세요')
      history.go(-1)
      </script>";
  }
  else {
    session_start();
    $user = mysql_fetch_array($result);
    $_SESSION['userid'= $id;
    $_SESSION['user_nic'= $user[2];
    echo "
    <script>
      location.href='index.php'
    </script>";
  }
}
mysql_close($connect);
?>
 
<meta charset="utf-8">
 
cs

 

회원가입

아이디 중복 확인, 비밀번호 확인을 하고 데이터베이스에 회원 정보를 넣는 부분

 

 

<sign_up.php> - 회원가입 ui, 아이디 중복확인, 비밀번호 확인

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>회원가입</title>
    <style media="screen">
    <style>
      * {margin: 0; padding: 0;}
      #sign_up_box{
        width:400px;
        height:150px;
        border:solid 2px gray;
        position: absolute;
        left: 50%; top: 50%;
        margin-left: -200px;
        margin-top: -100px;
        background-color: orange;
      }
      #sign_up_button{
        position: absolute;
        left: 40%; top: 80%;
      }
 
    </style>
    <script>
      function check_id(){
        var userid = document.getElementById("uid").value;
        if(userid)
           {
                 url = "check.php?userid="+userid;
                   window.open(url,"chkid","width=300,height=100");
             }
         else{
                    alert("아이디를 입력하세요");
                  }
            }
 
      function check_nik(){
        var usernic = document.getElementById("nic").value;
        if(usernic)
         {
             url = "check.php?usernic="+usernic;
             window.open(url,"chkid","width=300,height=100");
         }
         else{
              alert("닉네임을 입력하세요");
              }
          }
 
      function passwordCheck(){
          var pw = document.getElementById("pw").value;
          var pw_ck = document.getElementById("pw_ck").value;
          var id_ch = document.getElementById("id_ch").value;
          var nik_ch = document.getElementById("nik_ch").value;
          if (pw=="")  {
            alert("비밀번호를 입력해주세요.");
          }
          else if(id_ch==0){
            alert("아이디 중복확인을 해주세요");
          }
          else if(nik_ch==0){
            alert("닉네임 중복확인을 해주세요");
          }
          else if(pw != pw_ck){
            alert("비밀번호가 일치하지 않습니다 확인해 주세요.");
          }
          else{
            document.getElementById("sign").submit();
 
          }
      }
    </script>
  </head>
  <body>
    <div id="sign_up_box">
 
 
    <form class="" action="sign_up_search.php" method="post" id="sign">
      <table>
        <tr>
          <td>아이디</td>
          <td>
            <input type="text" name="id" id="uid">
          </td>
          <td>
            <button type="button" name="button" onclick="check_id()">중복확인</button>
            <input type="hidden" id="id_ch" name="" value="0">
          </td>
        </tr>
        <tr>
          <td>닉네임</td>
          <td>
            <input type="text" name="name" id="nic">
          </td>
          <td>
            <button type="button" name="button" onclick="check_nik()">중복확인</button>
            <input type="hidden" id="nik_ch" name="" value="0">
          </td>
        </tr>
        <tr>
          <td>비밀번호</td>
          <td>
            <input type="password" id="pw" name="passw">
          </td>
        </tr>
        <tr>
          <td>비밀번호 확인</td>
          <td>
            <input type="password" id="pw_ck" name="pass_check">
          </td>
        </tr>
      </table>
      <button id="sign_up_button" type="button" name="button" align="right" onclick="passwordCheck()">회원가입</button>
 
    </form>
    </div>
  </body>
</html>
 
cs

 

<sign_up_search.php> - 아이디, 비밀번호를 데이터베이스에 입력

1
2
3
4
5
6
7
8
9
10
11
12
13
<?
  include ('db_connect.php');
  $id = $_POST['id'];
  $nic = $_POST['name'];
  $pw = $_POST['passw'];
  $date=date("Y-m-d");
  $sql = mq("insert into user (id, nic_name, pw, user_date) values ('".$id."','".$nic."','".$pw."','".$date."')");
  echo "
  <script>
    location.href='index.php'
  </script>";
?>
 
cs

 

댓글