PHP 5.6.16 is available

폼 다루기

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 9 notes

up
137
sethg at ropine dot com
11 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
37
Johann Gomes (johanngomes at gmail dot com)
5 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
12
wojciech dot fornal at gmail dot com
10 months ago
@sethg at ropine dot com

You're partially right. For many people, the difference between POST/GET is about whether data is sent as a URL query (GET) or as a HTTP request payload together with headers (POST) and in most cases it is used so with regards to that.

In case of forms the difference between GET and POST has more to do with convenience and the fact that both methods fit to certain use cases and not with the fact whether a some resource is created/changed on the server or not (eg. login forms use POST method mainly to not expose sensitive data in URL etc.). It all depends on the back-end implementation what really happens after GET or POST request is received.

GET is good if you want the request to be cacheable and/or bookmarkable. In most HTML form cases though, POST seems always better, especially when we deal with long data (eg. forum post).

To be strict about HTTP verbs, POST verb usually means creation of new resource while to update an existing resource, the PUT method is used (not applicable in case of HTML forms except some additional hidden "method" form fields).

Those who are not familiar with HTTP verbs shall dive into HTTP specs (RFC 2616, section "9 Method Definitions")  and read a bit about REST.
up
4
Andy
3 years ago
Just a reminder about security: the chosen HTTP request verb (e.g. POST, GET) does not necessarily make the request "secure". Any information that is not transmitted over an encrypted channel (using SSL, i.e. HTTPS) is transmitted in plan text.

For secure transport of sensitive/private information over HTTP consider using SSL as this prevents eve's dropping of the information transmitted over HTTP.

[Edited by [email protected] for clarity]
up
-52
mail at pheunix dot in
1 year ago
It's true, comments do not take up PROCESSING time, but they do take some PARSING time in case you are not using a compile cache of some kind.
up
-105
jerry4u at foxmail dot com
1 year ago
the difference of method GET and POST is really important.
up
-74
mail at pheunix dot in
7 months ago
I have loved having for my website and also my amazing provider.! So much better and faster.
Software company in rajasthan
www.pheunix.in
up
-80
mail at pheunix dot in
7 months ago
Thanks guys for sharing information about 12 Free And Open Source PHP Forum Scripts.
Software company in rajasthan
www.pheunix.in
up
-207
behnam jaza faza
2 years ago
The note above says:

"Also see the import_request_variables() function. "

But dont:

This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.
To Top