보안을 그리다, 훈이

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

Programming/PHP & MySQL

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

HooNeee 2021. 5. 4. 20:30

이번 포스팅에서는 CRUD 중 게시글을 삭제하는 기능인 Delete에 대해 다루려고 한다.

 

이제 기본적인 기능은 대부분 다 구현한 것 같다ㅜㅜ

얼른 포스팅하고 놀아야지ㅣㅣㅣㅣㅣㅣㅣㅣㅣㅣㅣㅣㅣㅣㅣㅣㅣㅣㅣㅣㅣㅣ

 

당연하지만 delete.php에 게시글 삭제 기능이 구현되어 있다.

게시글 삭제는 사용자가 입력한 것들이 한 번에 삭제되기 때문에 매우 중요하고 위험한 작업일 수 있다.

따라서, 사용자로 행동을 한 번 더 검증하기로 했고, read.php 하단의 삭제 버튼에 confirm() 함수를 사용해 소스코드를 개선했다.

 

 

[read.php 개선 소스코드]

<!DOCTYPE html>
<html>

<head>
    <meta charset='utf-8'>

    <style>
        .read_table {
            border: 1px solid #444444;
            margin-top: 30px;
        }

        .read_title {
            height: 45px;
            font-size: 23.5px;
            text-align: center;
            background-color: #3C3C3C;
            color: white;
            width: 1000px;
        }

        .read_id {
            text-align: center;
            background-color: #EEEEEE;
            width: 30px;
            height: 33px;
        }

        .read_id2 {
            background-color: white;
            width: 60px;
            height: 33px;
            padding-left: 10px;
        }

        .read_hit {
            background-color: #EEEEEE;
            width: 30px;
            text-align: center;
            height: 33px;
        }

        .read_hit2 {
            background-color: white;
            width: 60px;
            height: 33px;
            padding-left: 10px;
        }

        .read_content {
            padding: 20px;
            border-top: 1px solid #444444;
            height: 500px;
        }

        .read_btn {
            width: 700px;
            height: 200px;
            text-align: center;
            margin: auto;
            margin-top: 40px;
        }

        .read_btn1 {
            height: 45px;
            width: 90px;
            font-size: 20px;
            text-align: center;
            background-color: #3C3C3C;
            border: 2px solid black;
            color: white;
            border-radius: 10px;
        }

        .read_comment_input {
            width: 700px;
            height: 500px;
            text-align: center;
            margin: auto;
        }

        .read_text3 {
            font-weight: bold;
            float: left;
            margin-left: 20px;
        }

        .read_com_id {
            width: 100px;
        }

        .read_comment {
            width: 500px;
        }
    </style>
</head>

<body>
    <?php
    $connect = mysqli_connect('127.0.0.1', 'root', 'password', 'db_board');
    $number = $_GET['number'];  // GET 방식 사용
    session_start();
    $query = "select title, content, date, hit, id from board where number = $number";
    $result = $connect->query($query);
    $rows = mysqli_fetch_assoc($result);

    $hit = "update board set hit = hit + 1 where number = $number";
    $connect->query($hit);

    if (isset($_SESSION['userid'])) {
    ?><b><?php echo $_SESSION['userid']; ?></b>님 반갑습니다.
        <button onclick="location.href='./logout_action.php'" style="float:right; font-size:15.5px;">로그아웃</button>
        <br />
    <?php
    } else {
    ?>
        <button onclick="location.href='./login.php'" style="float:right; font-size:15.5px;">로그인</button>
        <br />
    <?php
    }
    ?>

    <table class="read_table" align=center>
        <tr>
            <td colspan="4" class="read_title"><?php echo $rows['title'] ?></td>
        </tr>
        <tr>
            <td class="read_id">작성자</td>
            <td class="read_id2"><?php echo $rows['id'] ?></td>
            <td class="read_hit">조회수</td>
            <td class="read_hit2"><?php echo $rows['hit'] + 1 ?></td>
        </tr>


        <tr>
            <td colspan="4" class="read_content" valign="top">
                <?php echo $rows['content'] ?></td>
        </tr>
    </table>

    <div class="read_btn">
        <button class="read_btn1" onclick="location.href='./index.php'">목록</button>&nbsp;&nbsp;
        <?php
        if (isset($_SESSION['userid']) and $_SESSION['userid'] == $rows['id']) { ?>
            <button class="read_btn1" onclick="location.href='./modify.php?number=<?= $number ?>'">수정</button>&nbsp;&nbsp;
            <!-- 여기서부터 추가됨 -->
            <button class="read_btn1" a onclick="ask();">삭제</button>

            <script>
                function ask() {
                    if (confirm("게시글을 삭제하시겠습니까?")) {
                        window.location = "./delete.php?number=<?= $number ?>"
                    }
                }
            </script>
            <!-- 여기까지 -->
        <?php } ?>

    </div>
</body>

</html>

 

 

[read.php 개선 결과]

'게시글을 삭제하시겠습니까?'라는 confirm 창을 띄우고, 사용자가 취소 버튼을 클릭하면 당연하게도 아무런 작업을 수행하지 않는다.

반대로 확인 버튼을 클릭하면 delete.php로 접근한다.


[delete.php 소스코드]

<?php
$connect = mysqli_connect('127.0.0.1', 'root', 'password', 'db_board') or die("connect failed");
$number = $_GET['number'];

$query = "select id from board where number = $number";
$result = $connect->query($query);
$rows = mysqli_fetch_assoc($result);

$userid = $rows['id'];

session_start();

$URL = "./index.php";
?>

<?php
if (!isset($_SESSION['userid'])) {
?> <script>
        alert("권한이 없습니다.");
        location.replace("<?php echo $URL ?>");
    </script>

<?php } else if ($_SESSION['userid'] == $userid) {
    $query1 = "delete from board where number = $number";
    $result1 = $connect->query($query1); ?>
    <script>
        alert("게시글이 삭제되었습니다.");
        location.replace("<?php echo $URL ?>");
    </script>

<?php } else { ?>
    <script>
        alert("권한이 없습니다.");
        location.replace("<?php echo $URL ?>");
    </script>
<?php }
?>

 

마찬가지로 세션을 통해 권한을 통제하며, 게시자가 아닌 기타 회원 및 비회원이 접근하면 권한이 없다는 alert 창을 생성하고 다시 게시글로 복귀한다.

 

게시자가 confirm 창의 확인 버튼을 클릭하게 되면, delete문 쿼리를 통해 DB에서 게시글 튜플이 삭제되고 index.php로 이동한다.

 

 

[delete.php 결과]

Good~! 잘 작동한다.

 

MySQLi, PHP 등을 공부하면서 정이 많이 들었다.. 사실 코드가 잘 작동하지 않아 스트레스 받은 적도 있었지만 잘 마무리돼서 다행이라 생각한다. 구현은 빠르게 마무리하고 Web Hacking 공부로 넘어가야 되는데 개발도 하고싶다,,,ㅎ

Comments