Stack/PHP

[PHP] get / post 전송 방식

7ingout 2022. 5. 25. 12:19

1. get

보안에 취약해서 '조회'할 때 쓰임

http://localhost/php/form/number.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="number.php" method="get">
        <input type="number" name="num1" max="20" min="1">
        <input type="number" name="num2" max="20" min="1">
        <button type="submit">전송</button>
        <button type="reset">취소</button>
    </form>
</body>
</html>

 

number.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
        $num1 = $_GET['num1'];
        $num2 = $_GET['num2'];
    ?>
    <p>
        <?php
            if($num1 == $num2) {
                echo "num1의 값은 {$num1}이고 num2의 값은 {$num2}이며 같습니다.";
            } elseif($num1 > $num2) {
                echo "num1의 값은 {$num1}이고 num2의 값은 {$num2}이며 num1이 더 큽니다.";
            } else {
                echo "num1의 값은 {$num1}이고 num2의 값은 {$num2}이며 num2가 더 큽니다.";
            }
        ?>
    </p>
</body>
</html>

 

 

 

2. post

글을 수정하거나 회원가입 할 경우 ...,

http://localhost/php/form/number.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="number.php" method="post">
        <input type="number" name="num1" max="20" min="1">
        <input type="number" name="num2" max="20" min="1">
        <button type="submit">전송</button>
        <button type="reset">취소</button>
    </form>
</body>
</html>

 

number.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
        $num1 = $_POST['num1'];
        $num2 = $_POST['num2'];
        var_dump($_POST);
    ?>
    <p>
        <?php
            if($num1 == $num2) {
                echo "num1의 값은 {$num1}이고 num2의 값은 {$num2}이며 같습니다.";
            } elseif($num1 > $num2) {
                echo "num1의 값은 {$num1}이고 num2의 값은 {$num2}이며 num1이 더 큽니다.";
            } else {
                echo "num1의 값은 {$num1}이고 num2의 값은 {$num2}이며 num2가 더 큽니다.";
            }
        ?>
    </p>
</body>
</html>

 


 

 

HTML input태그의 name이 PHP 받아오는 변수 배열의 키 값으로 들어감 !