Form Kullanımı

PHP'nin en güçlü özelliklerinden biri HTML formlarına yaklaşım biçimidir. Bilinmesi gereken ilk önemli durum, form içindeki tüm elemanların PHP tarafından otomatik olarak kullanılabilir olacağıdır. PHP ile formların kullanımı ve daha ayrıntılı bilgi için Dış kaynaklı değişkenler bölümünü okuyabilirsiniz. Örnek bir HTML formu:

Örnek 1 - Örnek bir HTML formu

<form action="action.php" method="post">
 <p>İsminiz: <input type="text" name="isim" /></p>
 <p>Yaşınız: <input type="text" name="yaş" /></p>
 <p><input type="submit" /></p>
</form>

Bu formda özel hiçbir şey yoktur. Hiçbir özel etiket içermeyen düz bir HTML formudur. Kullanıcı formu doldurup gönder tuşuna bastığında, action.php sayfası çağrılır. Bu dosyaya aşağıdakileri yazabiliriz:

Örnek 2 - Formdan veri yazdırmak

Merhaba <?php echo htmlspecialchars($_POST['isim']); ?>.
Siz <?php echo (int)$_POST['yaş']; ?> yaşındasınız.

Bu betikten elde edilecek örnek çıktı:

Merhaba Ahmet. Siz 22 yaşındasınız.

htmlspecialchars() ve (int) kısımları harcinde yapılan iş oldukça açık. htmlspecialchars() işlevi HTML'ye özel karakterlerin doğru şekilde kodlandığından emin olunmasını sağlar, dolayısıyla başkaları sayfanıza dışardan HTML etiketleri veya Javascript yerleştiremez. Yaş alanınında ise değerin bir tamsayı olması gerektiğini bildiğimiz için değeri int türüne dönüştürmekle otomatik olarak bu alana girilmesi olası başı boş karakterlerden de kurtulmuş olduk. Ayrıca, bunun PHP'de sizin yerinize otomatik olarak yapılmasını sağlamak için süzgeç eklentisini de kullanabilirdiniz. $_POST['isim'] değişkeni ve $_POST['yaş'] değişkenleri sizin yerinize PHP tarafından otomatik olarak oluşturulur. Daha önce $_SERVER süper küresel değişkenini kullanmıştık, yukarıda ise tüm POST verisini içeren $_POST süper küresel değişkenini tanımış olduk. Formumuz için tanımlı yöntemin POST oluşuna dikkat edin. GET yöntemini kullanmış olsaydık, form bilgilerimiz $_GET süper küresel değişkenine atanmış olacaktı. Bunların haricinde, istemciden gelen verinin hangi kaynaktan geldiği sizin için önemli değilse $_REQUEST süper küreselini de kullanabilirdiniz. Bu değişken GET, POST ve COOKIE verilerinin birleşiminden oluşur.

PHP içinde XForms öğelerini de kullanabilirsiniz, ancak başlangıç aşamasında çok iyi desteklenen HTML formları sizin işinizi görecektir. XForms ile çalışmak yeni başlayanlar için uygun olmasa da, ilginizi çekebilir. XForms ile çalışmak belgesinde bu konu ile ilgili daha fazla bilgi bulabilirsiniz.

add a note add a note

User Contributed Notes 3 notes

up
156
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
65
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
36
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