수업소개
PHP를 이용해서 불특정 다수가 웹을 통해서 글을 제출하는 방법을 살펴보겠습니다.
index.php
<?php function print_title(){ if(isset($_GET['id'])){ echo $_GET['id']; } else { echo "Welcome"; } } function print_description(){ if(isset($_GET['id'])){ echo file_get_contents("data/".$_GET['id']); } else { echo "Hello, PHP"; } } function print_list(){ $list = scandir('./data'); $i = 0; while($i < count($list)){ if($list[$i] != '.') { if($list[$i] != '..') { echo "<li><a href=\"index.php?id=$list[$i]\">$list[$i]</a></li>\n"; } } $i = $i + 1; } } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> <?php print_title(); ?> </title> </head> <body> <h1><a href="index.php">WEB</a></h1> <ol> <?php print_list(); ?> </ol> <a href="create.php">create</a> <h2> <?php print_title(); ?> </h2> <?php print_description(); ?> </body> </html>
create.php
<?php function print_title(){ if(isset($_GET['id'])){ echo $_GET['id']; } else { echo "Welcome"; } } function print_description(){ if(isset($_GET['id'])){ echo file_get_contents("data/".$_GET['id']); } else { echo "Hello, PHP"; } } function print_list(){ $list = scandir('./data'); $i = 0; while($i < count($list)){ if($list[$i] != '.') { if($list[$i] != '..') { echo "<li><a href=\"index.php?id=$list[$i]\">$list[$i]</a></li>\n"; } } $i = $i + 1; } } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> <?php print_title(); ?> </title> </head> <body> <h1><a href="index.php">WEB</a></h1> <ol> <?php print_list(); ?> </ol> <a href="create.php">create</a> <form action="create_process.php" method="post"> <p> <input type="text" name="title" placeholder="Title"> </p> <p> <textarea name="description" placeholder="Description"></textarea> </p> <p> <input type="submit"> </p> </form> </body> </html>
create_process.php
<?php file_put_contents('data/'.$_POST['title'], $_POST['description']); header('Location: /index.php?id='.$_POST['title']); ?>