WEB/Server

PHP ๋‚ด์šฉ์ •๋ฆฌ - 4

kite707 2021. 1. 15.

 

https://opentutorials.org/course/3130/19385

 

PHP์—์„œ FORM๊ณผ POST - ์ƒํ™œ์ฝ”๋”ฉ

์ˆ˜์—…์†Œ๊ฐœ ์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” HTML์˜ ๊ธฐ๋Šฅ์ธ form์„ ์‚ดํŽด๋ด…๋‹ˆ๋‹ค. ๋˜, URL์„ ํ†ตํ•˜์ง€ ์•Š๊ณ  ์€๋ฐ€ํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ POST ๋ฐฉ์‹๋„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.  form.html form.php

form๊ณผ post์— ๋Œ€ํ•ด ๋‹ค๋ฃจ๋Š” ์ˆ˜์—…์ด๋‹ค.

<!--form.html-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="form.php">
        <p><input type="text" name="title" placeholder="Title"></p>
        <p><textarea name="description"></textarea></p>
        <p><input type="submit"></p>
    </form>
</body>
</html>
//form.php
<?php
echo $_GET['title']."<br>";
echo $_GET['description']."<br>";
?>

 

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ 

์ด๋ ‡๊ฒŒ ์ž…๋ ฅํ•œ ๋’ค ์—”ํ„ฐ๋ฅผ ๋ˆ„๋ฅด๋ฉด 

์ถœ๋ ฅํ™”๋ฉด์„ ๋ณด์•„ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž˜ ์ „๋‹ฌ๋˜์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

file_put_contentsํ•จ์ˆ˜

์‚ฌ์šฉ๋ฐฉ๋ฒ•

file_put_contents("ํŒŒ์ผ ๊ฒฝ๋กœ","๋„ฃ์„ ๋‚ด์šฉ");

์‚ฌ์šฉ์˜ˆ์‹œ

์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜๊ณ 

<!--form.html-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="form.php" method="post">
        <p><input type="text" name="title" placeholder="Title"></p>
        <p><textarea name="description"></textarea></p>
        <p><input type="submit"></p>
    </form>
</body>
</html>
//form.html
<?php
file_put_contents('data/'.$_POST['title'], $_POST['description']);
echo "description : ".file_get_contents('data/'.$_POST['title']);
?>

์ด๋ ‡๊ฒŒ ์ž…๋ ฅํ•˜๋ฉด 

 

์œ„์™€ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•๊ณผ ๋‹ค๋ฅธ ์ ์€ ๋งํฌ๊ฐ€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๊ฒƒ์ด๋‹ค. URLํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ urlํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ด์šฉํ•ด ๊ฐ’์„ ๊ฐ€์ ธ์™”๊ณ , ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ file_put_contentsํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์„ ํŒŒ์ผ์— ์ €์žฅํ•œ ๋’ค ๊ทธ ๊ฐ’์„ file_put_contentsํ•จ์ˆ˜๋กœ ๊ฐ€์ ธ์˜จ ๊ฒƒ์ด๋‹ค.


PHP๋กœ ๊ธ€ ์ƒ์„ฑํ•˜๊ธฐ

์ฝ”๋“œ๋Š” ์ด์ „๊ณผ ๋™์ผํ•˜๊ณ  headerํ•จ์ˆ˜๊ฐ€ ์ƒˆ๋กœ ๋“ฑ์žฅํ•œ๋‹ค.

headerํ•จ์ˆ˜

header("HTTP/1.0 404 Not Found");
header('Location: ํ•ด๋‹น ์ฃผ์†Œ ๋งํฌ');
//์ด์ „์˜ 3xx์ƒํƒœ ์ฝ”๋“œ๋ฅผ ์„ค์ •ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด REDIRECT(302)์ƒํƒœ ์ฝ”๋“œ๋ฅผ ๋ธŒ๋ผ์šฐ์ €์— ๋ฐ˜ํ™˜

์ฒซ๋ฒˆ์งธ์™€ ๊ฐ™์ด "HTTP/"๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ํ—ค๋”๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, ๋‘๋ฒˆ์งธ์™€ ๊ฐ™์ด ์ง์ ‘ ์ฃผ์†Œ๋ฅผ ๋„ฃ๊ธฐ๋„ ํ•œ๋‹ค. 

์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์˜ ๊ฒฝ์šฐ ์ฃผ๋กœ ์†ก์‹ ํ•˜๋Š” HTTP status์ฝ”๋“œ๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์˜ ๊ฒฝ์šฐ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•˜๋ฉฐ ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ REDIRECT (302)status๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

๊ฐ•์˜์—์„œ๋Š” ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค.

๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋ž€? 
re(๋‹ค์‹œ) + ์ง€์‹œํ•˜๋‹ค(direct)
์˜ˆ๋ฅผ๋“ค์–ด ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์›น์„œ๋ฒ„์— www.A๋ฅผ ์š”์ฒญํ–ˆ์„ ๋•Œ ์„œ๋ฒ„๊ฐ€ HTTP์‘๋‹ต ๋ฉ”์‹œ์ง€๋ฅผ ํ†ตํ•ด www.B๋กœ ์š”์ฒญํ•˜๋ผ๊ณ  ๋‹ค์‹œ ์ง€์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋ผ๊ณ  ํ•œ๋‹ค. 
์˜ˆ๋ฅผ ๋“ค์–ด ๋กœ๊ทธ์ธ์„ ํ•˜์ง€ ์•Š๊ณ  ๊ฒฐ์ œ๋ฅผ ํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ผ์ด ์ผ์–ด๋‚œ๋‹ค.
๊ฒฐ์ œ์‹œ๋„ -> ์„œ๋ฒ„๊ฐ€ ๋กœ๊ทธ์ธํ•˜์ง€ ์•Š์Œ์„ ํ™•์ธ ->๋กœ๊ทธ์ธ์ฐฝ์œผ๋กœ ๋Œ๋ ค๋ณด๋ƒ„(๋ฆฌ๋‹ค์ด๋ ‰ํŠธ) -> ๋กœ๊ทธ์ธ์™„๋ฃŒ ->๊ฒฐ์ œ

 

๊ฒฐ๊ณผ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

<?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>
    <?php
        print_description(); //์„ค๋ช…์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
        ?>
  </body>
</html>
<?php
file_put_contents("data/".$_POST['title'],$_POST['description']);
header("Location: /index.php?id=".$_POST['title']); //์‚ฌ์šฉ์ž๋ฅผ ํ•ด๋‹น ๋งํฌ๋กœ ์ด๋™ํ•˜๋„๋ก ํ•จ
?>

์ด๋ ‡๊ฒŒ ์ž…๋ ฅํ•˜๊ณ  ๊ฒฐ๊ณผ์ฐฝ์„ ๋ณด๋ฉด

์ด๋ ‡๊ฒŒ ์ž…๋ ฅ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ œ์ถœ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด

 

์ด๋ ‡๊ฒŒ ๋ชฉ๋ก 11๋ฒˆ์— try3๊ฐ€ ์ƒ๊ธฐ๊ณ (ํŒŒ์ผ์ด ์ œ๋Œ€๋กœ ์ƒ์„ฑ๋˜์—ˆ์Œ์„ ํ™•์ธ ๊ฐ€๋Šฅ) ์ œ์ถœ๋ฒ„ํŠผ ์•„๋ž˜์— try3์˜ description์ด ์ž˜ ์ถœ๋ ฅ ๋˜์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ„์˜ ๋งํฌ๋ฅผ ๋ณด๋ฉด id=try3์ธ๊ณณ์œผ๋กœ ์ž˜ ์ด๋™๋˜์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.(headerํ•จ์ˆ˜๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•จ)


PHP๋กœ ๊ธ€ ์ˆ˜์ •ํ•˜๊ธฐ

์šฐ์„  create๋ฐ‘์— ์ƒ์ž์™€ ์ œ์ถœ๋ฒ„ํŠผ์„ ์ง€์šฐ๊ณ , create์˜†์— ์ œ์ถœ๋ฒ„ํŠผ์„ ๋งŒ๋“ค ๊ฒƒ์ด๋‹ค. ๋˜ WEB์„ ๋ˆŒ๋ €์„ ๋•Œ๋Š” ๊ธฐ๋ณธ ํŽ˜์ด์ง€๋‹ˆ๊นŒ update๋ฒ„ํŠผ์ด ๋ณด์ด์ง€ ์•Š๋„๋ก ํ•  ๊ฒƒ์ด๋‹ค. ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

<?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>
    <?php if(isset($_GET['id'])){ ?>
        <a href="update.php">Update</a> <!--update๋งํฌ ์ƒ์„ฑ ์œ„ํ•œ ๋ถ€๋ถ„-->
    <?php } ?>
    
    <h2>
    <?php
        print_description(); //์„ค๋ช…์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
        ?>
    </h2>
  </body>
</html>

 

์ž˜ ๊ตฌํ˜„์ด ๋˜์—ˆ๋‹ค.

์ด์ œ ์ˆ˜์ • ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด ๋ณด๋„๋ก ํ•˜์ž. ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

<!--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>
    <?php if(isset($_GET['id'])){ ?>
        <a href="update.php?id=<?php echo $_GET['id']; ?>">Update</a> <!--update๋งํฌ ์ƒ์„ฑ ์œ„ํ•œ ๋ถ€๋ถ„-->
        <!-- <a href="update.php?id=<?=$_GET['id']?>">Update</a>  ๊ฐ™์€ ์ฝ”๋“œ-->
    <?php } ?>
    
    <h2>
    <?php
        print_description(); //์„ค๋ช…์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
        ?>
    </h2>
  </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']); //์‚ฌ์šฉ์ž๋ฅผ ํ•ด๋‹น ๋งํฌ๋กœ ์ด๋™ํ•˜๋„๋ก ํ•จ
?>
<!--update.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>
    <?php if(isset($_GET['id'])){ ?>
        <a href="update.php?id=<?php echo $_GET['id']; ?>">Update</a> <!--update๋งํฌ ์ƒ์„ฑ ์œ„ํ•œ ๋ถ€๋ถ„-->
        <!-- <a href="update.php?id=<?=$_GET['id']?>">Update</a>  ๊ฐ™์€ ์ฝ”๋“œ-->
    <?php } ?>
    
    <h2>
    <?php
        print_description(); //์„ค๋ช…์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
        ?>
        <form action="update_process.php" method="post">
            <input type="hidden" name="old_title" value="<?=$_GET['id'] ?>">
        <p>
            <input type="text" name="title" placeholder="Title" value="<?php print_title(); ?>"> 
        </p>
        <p>
            <textarea name="description" placeholder="Description"><?php print_description(); ?></textarea>
        </p>
        <p>
            <input type="submit">
        </p>
    </form>
    </h2>
  </body>
</html>
//update_process.php
<?php
rename('data/'.$_POST['old_title'], 'data/'.$_POST['title']);
file_put_contents('data/'.$_POST['title'],$_POST['description']);
?>

hi๋ฅผ ์„ ํƒํ•ด update๋ฅผ ๋ˆŒ๋ €๋‹ค.

 

์ œ์ถœ ํ›„ ์ƒˆ๋กœ๊ณ ์นจ์„ ํ•˜๋ฉด ์ œ๋ชฉ๊ณผ ์„ค๋ช…์ด ์ž…๋ ฅํ•œ๋Œ€๋กœ ๋ฐ”๋€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

PHP๋กœ ๊ธ€ ์‚ญ์ œํ•˜๊ธฐ

index.php๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•˜๊ณ  delete_process.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>
    <?php if(isset($_GET['id'])){ ?>
        <a href="update.php?id=<?php echo $_GET['id']; ?>">Update</a> <!--update๋งํฌ ์ƒ์„ฑ ์œ„ํ•œ ๋ถ€๋ถ„-->
        <!-- <a href="update.php?id=<?=$_GET['id']?>">Update</a>  ๊ฐ™์€ ์ฝ”๋“œ-->
        <a href="delete_process.php?id=<?=$_GET['id']?>">delete</a>
    <?php } ?>
    
    <h2>
    <?php
        print_description(); //์„ค๋ช…์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
        ?>
    </h2>
  </body>
</html>
//delete_process.php
<?php
unlink('data/'.$_GET['id']);
header('Location: /index.php'); //์‚ญ์ œ ํ›„ ํ™ˆํ™”๋ฉด์œผ๋กœ ์ด๋™
?>

ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด์ž.

ํ˜„์žฌ HTML22๋ฅผ ์„ ํƒํ•œ์ƒํ™ฉ์ด๋‹ค(์œ„์— URL์ฐธ๊ณ )
delete๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด์ž HTML22๊ฐ€ ์—†์–ด์ง€๊ณ  ํ™ˆ ํ™”๋ฉด์œผ๋กœ ๋Œ์•„์™”๋‹ค.

 

๋Œ“๊ธ€