폼 다루기

PHP의 매우 강력한 기능의 하나는 HTML 폼을 다루는 방법입니다. 이를 이해하는데에 중요한 기본적인 컨셉은 어떤 폼 요소라도 자동적으로 PHP 스크립트에서 사용 가능하다는 점입니다. PHP로 폼을 이용하는 많은 정보와 예제를 위해서 매뉴얼의 외부 변수 섹션을 읽어보십시오. 다음은 HTML 폼의 예제입니다:

Example #1 간단한 HTML 폼

<form action="action.php" method="post">
 <p>이름: <input type="text" name="name" /></p>
 <p>연령: <input type="text" name="age" /></p>
 <p><input type="submit" /></p>
</form>

이 폼에는 특별한 것은 아무것도 없습니다. 어떠한 특별한 태그도 가지지 않는 단순한 HTML 폼입니다. 유저가 이 폼을 채우고 submit 버튼을 누르면, action.php 페이지가 호출됩니다. 이 파일은 다음처럼 작성할 수 있습니다:

Example #2 폼에서 온 데이터 출력하기

<?php echo htmlspecialchars($_POST['name']); ?>씨 안녕하세요.
당신은 <?php echo (int)$_POST['age']; ?>세입니다.

이 스크립트의 출력 예제:

홍길동씨 안녕하세요. 당신은 22세입니다.

htmlspecialchars()(int) 부분을 제외하면, 이 소스가 어떤 일을 하는지는 명백합니다. htmlspecialchars()는 HTML에서 특별한 의미를 가지는 문자들을 정확히 인코딩하게 하여, HTML 태그나 자바스크립트를 페이지에 삽입할 수 없도록 합니다. age 필드에 대해서는, 숫자이여야 함을 알고 있으므로 간단히 integer변환하여 다른 문자들을 제거합니다. 이러한 작업은 필터 확장을 이용하여 PHP가 자동으로 하도록 할 수 있습니다. $_POST['name']$_POST['age'] 변수는 PHP가 자동적으로 사용할수 있도록 설정합니다. 위에서 자동전역 $_SERVER를 사용했듯이, 이번에는 모든 POST 데이터를 포함하는 $_POST 자동전역을 소개합니다. 폼에서 method를 POST로 설정한 점에 주의하십시오. 폼에서 GET method로 지정하면, 폼 정보는 $_GET 자동전역이 가집니다. 요청한 데이터가 어떤 소스인지를 신경쓰지 않을 때는 $_REQUEST 자동전역을 사용할 수 있습니다. 이는 GET, POST, COOKIE 데이터를 포함합니다. import_request_variables() 함수를 참고하십시오.

한동안은 잘 지원되는 HTML 폼에 만족할 수도 있지만, PHP에서는 XForms 입력을 다룰 수도 있습니다. XForms로 작업하는 것은 초보자를 위한 것은 아니지만, 흥미를 가질 것입니다. 기능 섹션에서 XForms로 받은 데이터를 다루는 짧은 안내를 참고하십시오.

add a note add a note

User Contributed Notes 3 notes

up
162
sethg at ropine dot com
20 years ago
According to the HTTP specification, you should use the POST method when you're using the form to change the state of something on the server end. For example, if a page has a form to allow users to add their own comments, like this page here, the form should use POST. If you click "Reload" or "Refresh" on a page that you reached through a POST, it's almost always an error -- you shouldn't be posting the same comment twice -- which is why these pages aren't bookmarked or cached.

You should use the GET method when your form is, well, getting something off the server and not actually changing anything.  For example, the form for a search engine should use GET, since searching a Web site should not be changing anything that the client might care about, and bookmarking or caching the results of a search-engine query is just as useful as bookmarking or caching a static HTML page.
up
62
Johann Gomes (johanngomes at gmail dot com)
13 years ago
Also, don't ever use GET method in a form that capture passwords and other things that are meant to be hidden.
up
25
nucc1
6 years ago
worth clarifying:

POST is not more secure than GET.

The reasons for choosing GET vs POST involve various factors such as intent of the request (are you "submitting" information?), the size of the request (there are limits to how long a URL can be, and GET parameters are sent in the URL), and how easily you want the Action to be shareable -- Example, Google Searches are GET because it makes it easy to copy and share the search query with someone else simply by sharing the URL.

Security is only a consideration here due to the fact that a GET is easier to share than a POST. Example: you don't want a password to be sent by GET, because the user might share the resulting URL and inadvertently expose their password.

However, a GET and a POST are equally easy to intercept by a well-placed malicious person if you don't deploy TLS/SSL to protect the network connection itself.

All Forms sent over HTTP (usually port 80) are insecure, and today (2017), there aren't many good reasons for a public website to not be using HTTPS (which is basically HTTP + Transport Layer Security).

As a bonus, if you use TLS  you minimise the risk of your users getting code (ADs) injected into your traffic that wasn't put there by you.
To Top