티스토리 뷰

index.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
<html>
    <head>
    <title>Login Test Page</title>
    <meta http-equiv="Content-Type" content="text/html" />
    </head>
    <body>
    <form action="login.php" method="POST">
        <table>
        <tr>
           <td colspan="2">Login Page</td>
        </tr>
        <tr>
            <td>ID</td>
            <td><input type="text" name="id"></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type="password" name="pw"></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="Login">
                <input type="button" name="Join" value="Join" onclick="location.href='./join.php'";>
            </td>
        </tr>
        </table>
    </form>
    </body>
</html>
cs


conn.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
    try {
        $mongo = new MongoDB\Driver\Manager("mongodb://localhost:27017");
 
        $command = new MongoDB\Driver\Command(['ping' => 1]);
    } catch (MongoDB\Driver\Exception\Exception $e) {
        $filename = basename(__FILE__);
    
        echo "The $filename script has experienced an error.\n"
        echo "It failed with the following exception:\n";
        
        echo "Exception:"$e->getMessage(), "\n";
        echo "In file:"$e->getFile(), "\n";
        echo "On line:"$e->getLine(), "\n";       
    }
?>
cs


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
<html>
<head>
    <title>Login Check</title>
    <meta http-equiv="Content-Type" content="text/html"/>
</head>
<body>
<?php
    include "./conn.php";
 
    $id = $_POST['id'];
    $pw = $_POST['pw'];
 
    $filter = ['id' => $id'pw' => $pw];
    $options = [
        'projection' => ['_id' => 0]
    ];
 
    $query = new MongoDB\Driver\Query($filter$options);
    $rows = $mongo->executeQuery("login.user"$query);
 
    $login = current($rows->toArray());
    if(!empty($login)) {
        if(($id == $login->id) && ($pw == $login->pw)) {
            if($id == "admin") {
                echo "Welcome Admin!<br><br>";
            }
            echo "$id : $pw<br>";
            echo "<br>Login Success";
        }
    } else {
        echo "Login Failed...<br>";
    }
?>
</body>
</html>
 
cs


join.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
<html>
    <head>
    <title>Join Page</title>
    <meta http-equiv="Content-Type" content="text/html" />
    </head>
    <body>
    <form action="join.php" method="POST">
        <table>
        <tr>
            <td>ID</td>
            <td><input type="text" name="id"></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type="password" name="pw"></td>
        </tr>
        <tr>
            <td>Password Verify</td>
            <td><input type="password" name="pw2"></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="Join">
                <input type="reset" value="Reset";>
                <input type="button" name="Back" value="Back" onclick="location.href='./index.php'";>
            </td>
        </tr>
        </table>
    </form>
    </body>
</html>
<?php
    $id = $_POST['id'];
    $pw = $_POST['pw'];
    $pw2 = $_POST['pw2'];
 
    if(($id != null|| ($pw != null|| ($pw2 != null)) {
        include "./conn.php";
 
        $filter = ['id' => $id];
        $options = [
            'projection' => ['_id' => 0]
        ];
        $query = new MongoDB\Driver\Query($filter$options);
        $rows = $mongo->executeQuery("login.user"$query);
 
        $log = current($rows->toArray());
        if(!empty($log)) {
            echo "Sorry. $id already exists.<br>";
        } else {
            if($pw == $pw2) {
                $bulk = new MongoDB\Driver\BulkWrite;           
                $bulk->insert(['id' => $id'pw' => $pw]);
                $mongo->executeBulkWrite('login.user'$bulk);
 
                header('location:'."./index.php");
            } else {
                echo "Sorry. Password is not matched.<br>";
            }
        }
    }
?>
cs


SQL을 벗어나서 처음 코딩해본건데 제법 재밌었다. 오로지 영어로 써서 그런지 매번 코딩할 때 마다 주석을 안 달게 된다...

'Web' 카테고리의 다른 글

Apache2 cgi 동적 페이지  (2) 2017.04.05
NOSQL Injection from MongoDB  (0) 2017.03.28
MongoDB 설치 및 사용 방법  (0) 2017.03.26
php 에러 출력  (0) 2016.02.15
Mysql 데이터베이스 추가, 삭제  (0) 2016.02.15
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
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