html_entities

html_entities는 php의 함수로 각종 태그들을 html Entity로 변환하는 함수이다. 이를 통해 XSS를 방지 할 수 있다.


 

writer.php(게시물 등록)

<?php
  $isetTitle = isset($_POST['title']) && strlen($_POST['title']) > 0;
  $issetContent = isset($_POST['content']) && strlen($_POST['content']) > 0;
  $isSubmit = isset($_POST['is_submit']) && strlen($_POST['is_submit']) > 0;
 
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
    if ($isetTitle && $issetContent && $isSubmit) {

      // 저장하기
      $title = $_POST['title'];
      $content = $_POST['content'];
      echo "before:".$content.'<br>';
      // 게시물 등록 SQL로 데이터를 전달하기 전 
      // htmlentities함수를 사용하여 태그를 변환한다.
      $title = htmlentities($title);
      $content = htmlentities($content);
      // echo $title.'<br>';
      // echo "after:".$content.'<br>';
      $result = insert_tbl_board($title, $content);
      if($result) {
        echo "<script>alert('작성 완료.');
                location.href='/board/list.php'
                </script>";
                exit;
      }

    }
  }

?>

 

글쓰기에 script를 삽입 후 내용을 확인해보자

summernote의 태그는 &lt인데 내가 작성한 글은 lt;와 같은 방식으로 다르게 저장된다... 이유는 모르겠다...

아시는 분은 댓글 부탁드립니다.

script와 alert을 적용
변환된 태그들이 저장되었다.

 

조회 화면에서 decode를 하여 화면에 뿌리게되면.

list.php

<tbody>
          <?php
			// 입력받은 페이지
            $page = isset($_GET["page"])? $_GET["page"] : 1;
            // 입력받은 검색타입
            $searchType = isset($_GET["searchType"])? $_GET["searchType"] : '';
            //입력받은 검색키워드
            $searchValue = isset($_GET["searchValue"])? $_GET["searchValue"] : '';
            // 한 페이지당 보여줄 개수
            $itemPerPage = 10;
            
			// limit 0, 10은 0개를 패스하고 10개까지 보여준다.
            // 페이지가 2라면 10 개를 패스하고 10개를 보여줘야하기에 limit 10, 10이 되어야한다.
            $boardList = getBoardList(($page-1) * $itemPerPage, $itemPerPage, $searchType, $searchValue);

            while ($board =  mysqli_fetch_array($boardList)) 
            {
              $idx = $board['idx'];
              // htmlentitiy 디코드
              $title = html_entity_decode($board['title']);
              $content = html_entity_decode($board['content']);

              $regUser = $board['regUser'];
              $regTime = $board['regTime'];

              // var_dump($content);
              echo "<tr onclick='moveDetail({$idx})'>";
              echo "<th  class='idx' scope='row'>{$idx}</td>";
              echo "<td class='title' >{$title}</td>";
              echo "<td class='content'>{$content}</td>";
              echo "<td class='reg_user'>{$regUser}</td>";
              echo "<td class='reg_time'>{$regTime}</td>";
              echo "</tr>";
            }
            
          ?>

        </tbody>

스크립트가 삽입되어도 동작하지 않는다.....

+ Recent posts