보안을 그리다, 훈이

[Web Hacking / PHP & MySQL] 게시판 구현 - index.php 본문

Programming/PHP & MySQL

[Web Hacking / PHP & MySQL] 게시판 구현 - index.php

HooNeee 2021. 4. 28. 20:40

게시판의 목차를 보여주는 index.php부터 구현하였다.

 

[index.php 소스코드]

<!DOCTYPE html>
<html>

<head>
    <meta charset='utf-8'>
    <style>
        table {
            border-top: 1px solid #444444;
            border-collapse: collapse;
        }

        tr {
            border-bottom: 1px solid #444444;
            padding: 10px;
        }

        td {
            border-bottom: 1px solid #efefef;
            padding: 10px;
        }

        table .even {
            background: #efefef;
        }

        .text {
            text-align: center;
            padding-top: 20px;
            color: #000000
        }

        .text:hover {
            text-decoration: underline;
        }

        a:link {
            color: #57A0EE;
            text-decoration: none;
        }

        a:hover {
            text-decoration: underline;
        }
    </style>
</head>

<body>
    <?php
    $connect = mysqli_connect('127.0.0.1', 'root', 'password', 'db_board') or die("connect failed");
    $query = "select * from board order by number desc";    //역순 출력
    $result = mysqli_query($connect, $query);
    //$result = $connect->query($query);
    $total = mysqli_num_rows($result);  //result set의 총 레코드(행) 수 반환
    ?>
    <p style="font-size:25px; text-align:center"><b>게시판</b></p>
    <table align=center>
        <thead align="center">
            <tr>
                <td width="50" align="center">번호</td>
                <td width="500" align="center">제목</td>
                <td width="100" align="center">작성자</td>
                <td width="200" align="center">날짜</td>
                <td width="50" align="center">조회수</td>
            </tr>
        </thead>

        <tbody>
            <?php
            while ($rows = mysqli_fetch_assoc($result)) { //result set에서 레코드(행)를 1개씩 리턴
                if ($total % 2 == 0) {
            ?>
                    <tr class="even">
                        <!--배경색 진하게-->
                    <?php } else {
                    ?>
                    <tr>
                        <!--배경색 그냥-->
                    <?php } ?>
                    <td width="50" align="center"><?php echo $total ?></td>
                    <td width="500" align="center">
                        <a href="read.php?number=<?php echo $rows['number'] ?>">
                            <?php echo $rows['title'] ?>
                    </td>
                    <td width="100" align="center"><?php echo $rows['id'] ?></td>
                    <td width="200" align="center"><?php echo $rows['date'] ?></td>
                    <td width="50" align="center"><?php echo $rows['hit'] ?></td>
                    </tr>
                <?php
                $total--;
            }
                ?>
        </tbody>
    </table>

    <div class=text>
        <font style="cursor: hand" onClick="location.href='./write.php'">글쓰기</font>
    </div>
</body>

</html>

 

index.php는 게시판 메인 화면이며 글 번호, 제목, 작성자, 날짜, 조회수를 역순으로 출력했다.

게시판이므로 GET 방식이 아닌 POST 방식으로 구현했다.

 

 

[GET 방식과 POST 방식의 차이]

 

1. 서로 인코딩 방식이 다르다.

2. GET 방식은 전송속도가 빠른 반면, POST 방식은 보다 느리다.

3. history.back() 사용시 GET 방식은 이전 데이터를 보여주지만, POST 방식은 새로 요청한다.

4. GET 방식은 글자수 제한이 있지만 POST 방식은 글자수 제한이 없다.

5. GET 방식은 URL에 데이터가 출력되므로 보안에 취약하지만, POST 방식은 보다 안전하다.

6. GET 방식은 서버의 데이터를 가져오는 SELECT문에 적합하지만, POST 방식은 게시판의 글을 등록 및 수정하는 등의 작업에 적합하다.

 

 

[index.php 결과]

 

Comments