웹개발/PHP- 홈페이지 구현

로그인 페이지 구현

duduranran 2023. 1. 1. 21:37

오늘은 로그인 페이지를 구현했다. 못생겼다.

꾸미는 것보다는 내부적으로 어떻게 구현이 되는지 아는 것이 중요하다.

다음주 부터는 시큐어 코딩.

 

 

login_view.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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
 
    <title>loginPage</title>
</head>
 
<body>
    <div align="center">
 
    <form name = "login" action="1_login_server.php" method = "post">
    <h1 class="title">LOGIN PAGE</h1>
    <hr>
    <br>
    <table border ="1">
        <tr align="center">
            <td>아이디</td>
            <td colspan="2"><input type="text" placeholder ="ID" name ="user_id" autofocus></td>
        </tr>
        <tr align="center">
            <td>패스워드</td>
            <td colspan="2"><input type="password" placeholder ="Password" name ="user_pass"></td>
        </tr>
        <tr align="center">
            <td colspan="3">
            <button type = "submit" name ="login_btn">로그인</button>
            <input type="button", name ="register" value ="회원가입" onclick ="location.href ='./1_register_view.php'">
            <input type="button", name ="no_register" value ="비회원 Q&A" onclick ="location.href ='./QNA/q_board_main.php'">
            </td>
        </tr>
    </table>
    </form>
 
    </div>
</body>
</html>
cs

 

 

login_server.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
<?php
include("db.php");
//여기서 $_SESSION 지역변수를 사용하겠다고 시작점을 알려주는 것
session_start();   
 
// POST로 넘어온 값을 변수에 대입
$id = $_POST['user_id'];  
$pass = $_POST['user_pass'];
 
$id = mysqli_real_escape_string($conn$id); // 시큐어 코딩
$pass = mysqli_real_escape_string($conn$pass);
$pass = hash("sha256",$pass);  //패스워드 해쉬화
 
if(empty($_SESSION)){
    if(empty($id)){
        echo "<script>alert('아이디를 입력해 주세요.');</script>";
        echo "<script>location.replace('./1_login_view.php');</script>";
        exit();
    
    } else if(empty($pass)){
        echo "<script>alert('비밀번호를 입력해 주세요.');</script>";
        echo "<script>location.replace('./1_login_view.php');</script>";
        exit();
    
    } else {
        $sql = "select db_id,db_pw from basic_table where db_id ='$id'";  
        // basic_table에서 db_id의 데이터 중 입력받은 $id와 같은 게 있으면 가져와
        $result = mysqli_query($conn$sql);                    
        // 연결된 디비에 위의 SQL질의문을 실행하고, 그결과를 $result에 담자.  배열형태로 가져오게 됨
         if(mysqli_num_rows($result=== 1){      
            $row = mysqli_fetch_assoc($result);
            $bring_pass = $row['db_pw'];
         
            if($pass == $bring_pass){
                //DB에 저장된 패스워드와 사용자로부터 입력받은 값이 같다면
                //세션 발급 후 메인 페이지로 이동 시킨다.
                $_SESSION['id'= $row['db_id'];    
                $_SESSION['pw'= $row['db_pw']; 
                echo "<script>location.href='./2_main_page.php';</script>";
                exit();
                
            } else { // 패스워드가 맞지 않을 때
                echo "<script>alert('비밀번호를 확인해 주세요.');</script>"
                echo "<script>location.replace('./1_login_view.php');</script>";  // 패스워드 다시 확인 하라고 했으니 다시 로그인 뷰 페이지로 넘겨주면되겠따.
                exit();
            }
        } else {
                //echo 'Could not update data: '. mysql_error();
                echo "<script>alert('아이디를 확인해 주세요.');</script>";
                echo "<script>location.replace('./1_login_view.php');</script>";
                exit();
        } 
    } 
 
else {
    echo "<script>location.href='./2_main_page.php';</script>";
    exit;
}
 
 
 
?>
cs