PHP 8.3.4 Released!

Dış Kaynaklı Değişkenler

HTML Formları (GET ve POST)

Bir PHP betiğine bir form gönderildiğinde o formdaki bilgi betik tarafından otomatik olarak kullanılır. Bu bilgiye erişmek için birçok yol vardır, örneğin:

Örnek 1 - Basit bir HTML formu

<form action="foo.php" method="post">
Name: <input type="text" name="kullanici_adi" /><br />
Email: <input type="text" name="eposta" /><br />
<input type="submit" name="submit" value="Beni gönder!" />
</form>

HTML formlarınızdaki bilgiye erişimin iki yolu vardır. Şu an için kullanılabilen yöntemler aşağıda sıralanmıştır:

Örnek 2 - Basit bir POST HTML formundan bilgiye erişim

<?php

echo $_POST['kullanici_adi'];
echo $_REQUEST['kullanici_adi'];
?>

Bunun yerine önceden tanımlı uygun bir GET değişkeni kullanmak dışında, GET formunun kullanımı aynıdır. GET ayrıca QUERY_STRING (Bir URL'de '?' iminden sonraki bilgi) sorgu dizgesine de uygulanır. Bu bakımdan örneğin, http://mesela.dom/dnm.php?id=3 $_GET['id'] ile erişilebilen GET verisini içerir. Ayrıca, $_REQUEST değişkenine de bakınız.

Bilginize:

Değişken isimlerindeki noktalar ve boşluklar altçizgi karakterine dönüştürülür. Örneğin, <input name="a.b" /> ifadesi $_REQUEST["a_b"] haline gelir.

PHP form değişkenleri bağlamında dizileri de anlar (ilgili SSS'ye bakınız). Örneğin, ilgili değişkenleri birlikte gruplayabilir veya bu özelliği çoklu seçim girdisinden değerleri almak için kullanabilirsiniz. Örneğin, bir formu kendine gönderelim ve gönderilen veriyi gösterelim:

Örnek 3 - Daha karmaşık form değişkenleri

<?php
if ($_POST) {
echo
'<pre>';
echo
htmlspecialchars(print_r($_POST, true));
echo
'</pre>';
}
?>
<form action="" method="post">
Adı: <input type="text" name="personal[isim]" /><br />
Eposta: <input type="text" name="personal[eposta]" /><br />
Bira: <br />
<select multiple name="bira[]">
<option value="efes">Efes</option>
<option value="tuborg">Tuborg</option>
<option value="venus">Venüs</option>
</select><br />
<input type="submit" value="Gönder!" />
</form>

Bilginize: Harici değişken ismi geçerli bir dizi sözdizimi ile başlıyorsa sondaki karakterler sessizce yoksayılır. Örneğin, <input name="foo[bar]baz"> girdisi $_REQUEST['foo'] ['bar'] haline gelir.

Gönder düğmesi değişken isimleri

Bir formu gönderirken, şöyle bir etiketle standart gönder düğmesi yerine görüntü kullanılabilir:

<input type="image" src="image.gif" name="sub" />

Kullanıcı görüntü üzerinde bir yere tıkladığında, eşlik eden form iki ek değişkenle sunucuya iletilmiş olur: sub_x ve sub_y. Bunlar kullanıcın görüntü üzerinde tıkladığı noktanın koordinatlarını içerir. Deneyimliler tarayıcı tarafından gönderilen asıl değişken isimlerinin alt çizgi yerine nokta içerdiğine dikkat etmiş olabilirler, fakat PHP noktaları otomatik olarak alt çizgiye çevirir.

HTTP Çerezleri

PHP, » RFC 6265'de tanımlandığı gibi HTTP çerezlerini şeffaf olarak destekler. Çerezler kullanıcıları izleyip tanımlamak amacıyla kullanıcının tarayıcısında veri saklamak için kullanılan bir yöntemdir. Çerezleri setcookie() işlevini kullanarak belirtebilirsiniz. Çerezler HTTP başlığının bir parçasıdır, bu nedenle setcookie() işlevi tarayıcıya herhangi bir çıktı gönderilmeden önce çağrılmak zorundadır. Bu kısıtlama header() işlevi için de geçerlidir. Çerez verisi $_COOKIE veya $_REQUEST gibi uygun çerez veri dizisinde bulunur. Ayrıntılar ve örnekler için setcookie() kılavuz sayfasına bakınız.

Bilginize: Sırasıyla PHP 7.2.34, 7.3.23 ve 7.4.11'den itibaren, gelen çerezlerin isimleri güvenlik kaygıları nedeniyle artık url-kodlu değildir.

Eğer tek bir çerez değişkenine birden fazla değer atamak isterseniz, değişkeni bir dizi olarak atayabilirsiniz. Örneğin:

<?php
setcookie
("Cerezim[foo]", 'Deneme 1', time()+3600);
setcookie("Cerezim[bar]", 'Deneme 2', time()+3600);
?>

Her ne kadar betiğinizde Cerezim tek bir dizi olsa da bu iki ayrı çerez oluşturacaktır. Eğer birden fazla değere sahip tek bir çerez atamak isterseniz, önce değer üzerinde serialize() veya explode() kullanmayı düşünün.

Yolu veya alan adı farklı olmadığı sürece aynı isimdeki bir çerez önceki çerezin üzerine yazılır. Böylece, isterseniz örneğin bir alışveriş sepeti uygulaması için bir sayaç tutabilir ve bunu karşıya aktarabilirsiniz.

Örnek 4 - setcookie() örneği

<?php
if (isset($_COOKIE['sayac'])) {
$sayac = $_COOKIE['sayac'] + 1;
} else {
$sayac = 1;
}
setcookie('sayac', $sayac, time()+3600);
setcookie("Sepet[$sayac]", $mal, time()+3600);
?>

Gelen değişken isimleri içindeki noktalar

Genellikle, PHP bir betiğe aktarılan değişken isimlerini değiştirmez. Fakat, nokta iminin PHP'de değişken isimlerinde geçerli bir karakter olmadığına dikkat edilmelidir. Örnek:

<?php
$varname
.ext; /* geçersiz değişken ismi */
?>
Burada, yazım denetleyicinin gördüğü $varname isimli bir değişkeni takip eden bir dizge birleştirme işlemi ve çıplak bir (tırnaklarla çevrilmemiş, herhangi bir anahtar veya anahtar sözcük ile eşleşmeyen dizge) 'ext' metnidir. Açıkça görülüyor ki, bu istenen sonucu vermemektedir.

Bu nedenle, PHP'nin gelen değişken isimlerindeki nokta imlerini alt çizgi imi ile değiştirdiğini bilmekte yarar vardır.

Değişken türlerini belirlemek

Değişken türlerini PHP belirlediği ve (genellikle) gerektiği gibi değiştirdiği için, verilen bir değişkenin herhangi bir anda hangi türde olduğu sorgulanmadan bilinemez. PHP'de bir değişkenin hangi türde olduğunu bulan birçok işlev vardır. Örnek: gettype(), is_array(), is_float(), is_int(), is_object() ve is_string(). Ayrıca Türler bölümüne de bakınız.

Bir metin protokolü olarak HTTP'de, çoğunlukla, tamamen olmasa da, Süper küresel diziler içinde gelen içerik dizge olarak kalır. PHP değerleri belli bir türe dönüştürmeyi denemez. Aşağıdaki örnekte, $_GET["var1"] değişkeni "null" ve $_GET["var2"]ise "123" dizgesini içerecektir.

/index.php?var1=null&var2=123

Sürüm Bilgisi

Sürüm: Açıklama
7.2.34, 7.3.23, 7.4.11 Gelen çerezlerin isimleri güvenlik kaygıları nedeniyle artık url-kodlu değildir.

add a note

User Contributed Notes 2 notes

up
17
Anonymous
16 years ago
The full list of field-name characters that PHP converts to _ (underscore) is the following (not just dot):
chr(32) ( ) (space)
chr(46) (.) (dot)
chr(91) ([) (open square bracket)
chr(128) - chr(159) (various)

PHP irreversibly modifies field names containing these characters in an attempt to maintain compatibility with the deprecated register_globals feature.
up
6
krydprz at iit dot edu
18 years ago
This post is with regards to handling forms that have more than one submit button.

Suppose we have an HTML form with a submit button specified like this:

<input type="submit" value="Delete" name="action_button">

Normally the 'value' attribute of the HTML 'input' tag (in this case "Delete") that creates the submit button can be accessed in PHP after post like this:

<?php
$_POST
['action_button'];
?>

We of course use the 'name' of the button as an index into the $_POST array.

This works fine, except when we want to pass more information with the click of this particular button.

Imagine a scenario where you're dealing with user management in some administrative interface. You are presented with a list of user names queried from a database and wish to add a "Delete" and "Modify" button next to each of the names in the list. Naturally the 'value' of our buttons in the HTML form that we want to display will be "Delete" and "Modify" since that's what we want to appear on the buttons' faceplates.

Both buttons (Modify and Delete) will be named "action_button" since that's what we want to index the $_POST array with. In other words, the 'name' of the buttons along cannot carry any uniquely identifying information if we want to process them systematically after submit. Since these buttons will exist for every user in the list, we need some further way to distinguish them, so that we know for which user one of the buttons has been pressed.

Using arrays is the way to go. Assuming that we know the unique numerical identifier of each user, such as their primary key from the database, and we DON'T wish to protect that number from the public, we can make the 'action_button' into an array and use the user's unique numerical identifier as a key in this array.

Our HTML code to display the buttons will become:

<input type="submit" value="Delete" name="action_button[0000000002]">
<input type="submit" value="Modify" name="action_button[0000000002]">

The 0000000002 is of course the unique numerical identifier for this particular user.

Then when we handle this form in PHP we need to do the following to extract both the 'value' of the button ("Delete" or "Modify") and the unique numerical identifier of the user we wish to affect (0000000002 in this case). The following will print either "Modify" or "Delete", as well as the unique number of the user:

<?php
$submitted_array
= array_keys($_POST['action_button']);
echo (
$_POST['action_button'][$submitted_array[0]] . " " . $submitted_array[0]);
?>

$submitted_array[0] carries the 0000000002.
When we index that into the $_POST['action_button'], like we did above, we will extract the string that was used as 'value' in the HTML code 'input' tag that created this button.

If we wish to protect the unique numerical identifier, we must use some other uniquely identifying attribute of each user. Possibly that attribute should be encrypted when output into the form for greater security.

Enjoy!
To Top