[Web Hacking / PHP & MySQL] 게시판 구현 - write.php / write_action.php
이번에는 게시판의 존재 목적인 CRUD 중 필자가 가장 중요하다고 생각하는 Create에 대해 다뤄볼 것이다. (물론 Read도 중요하다ㅎㅎ)
글을 작성하기 위한 form인 write.php를 구현한 후, 작성한 글을 게시판에 업로드하는 action을 취해줄 write_action.php도 구현하였다.
[write.php 소스코드]
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<style>
table.table2 {
border-collapse: separate;
border-spacing: 1px;
text-align: left;
line-height: 1.5;
border-top: 1px solid #ccc;
margin: 20px 10px;
}
table.table2 tr {
width: 50px;
padding: 10px;
font-weight: bold;
vertical-align: top;
border-bottom: 1px solid #ccc;
}
table.table2 td {
width: 100px;
padding: 10px;
vertical-align: top;
border-bottom: 1px solid #ccc;
}
</style>
</head>
<body>
<form method="post" action="write_action.php">
<!-- method : POST!!! (GET X) -->
<table style="padding-top:50px" align=center width=auto border=0 cellpadding=2>
<tr>
<td style="height:40; float:center; background-color:#3C3C3C">
<p style="font-size:25px; text-align:center; color:white; margin-top:15px; margin-bottom:15px"><b>게시글 작성하기</b></p>
</td>
</tr>
<tr>
<td bgcolor=white>
<table class="table2">
<tr>
<td>작성자</td>
<td><input type="text" name="name" size=30></td>
</tr>
<tr>
<td>제목</td>
<td><input type="text" name="title" size=70></td>
</tr>
<tr>
<td>내용</td>
<td><textarea name="content" cols=75 rows=15></textarea></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="password" name="pw" size=15 maxlength=15></td>
</tr>
</table>
<center>
<input style="height:26px; width:47px; font-size:16px;" type="submit" value="작성">
</center>
</td>
</tr>
</table>
</form>
</body>
</html>
write.php는 index.php의 [글쓰기] 버튼을 통해 불러오며, write.php에는 작성자, 제목, 내용, 비밀번호를 입력할 수 있다.
보안상 중요한 글쓰기 기능이므로 method는 GET 방식이 아닌 POST 방식을 사용한다.
[write.php 결과]
[write_action.php 소스코드]
<?php
$connect = mysqli_connect("127.0.0.1", "root", "password", "db_board") or die("fail");
$id = $_POST['name']; //Writer
$pw = $_POST['pw']; //Password
$title = $_POST['title']; //Title
$content = $_POST['content']; //Content
$date = date('Y-m-d H:i:s'); //Date
$URL = './index.php'; //return URL
$query = "INSERT INTO board (number, title, content, date, hit, id, password)
values(null,'$title', '$content', '$date', 0, '$id', '$pw')";
$result = $connect->query($query);
if ($result) {
?> <script>
alert("<?php echo "게시글이 등록되었습니다." ?>");
location.replace("<?php echo $URL ?>");
</script>
<?php
} else {
echo "게시글 등록에 실패하였습니다.";
}
mysqli_close($connect);
?>
write_action.php 또한 마찬가지로 POST 방식으로 값을 제공받아 게시판에 글을 등록한다.
[write_action.php 결과]
[DB 조회 결과]
SELECT문으로 board 테이블을 조회해본 결과, 게시글이 정상적으로 DB에 입력된 것을 확인할 수 있다 :)
password 속성에 1234가 그대로 출력되는 것과 같이 password가 평문으로 DB에 저장되는 시스템은 매우 취약하므로 실제 서비스에서 사용하면 큰일난다!
추후 시큐어코딩을 통해 보완할 예정이다.
+ 필자는 개발 및 보안 공부를 목적으로 하기에 취약점 보완을 추후에 진행할 예정이지만, 실제 서비스를 목적으로 개발하는 소프트웨어는 KISA에서 발간하는 '소프트웨어 개발 보안 가이드'를 참고하여 개발 초기부터 보안을 고려해야만 한다.