Note that this function just does what the documentation says: It escapes special characters in strings.
It does NOT - however - detect a "NULL" value. If the value you try to quote is "NULL" it will return the same value as when you process an empty string (-> ''), not the text "NULL".
PDO::quote
(PHP 5 >= 5.1.0, PECL pdo >= 0.2.1)
PDO::quote — Bir sorguda kullanılmak üzere bir dizgeyi önceler
Açıklama
$dizge
[, int $değiştirge_türü = PDO::PARAM_STR
] )Girdi dizgesine gerekiyorsa ilgili veritabanı sürücüsüne uygun bir önceleme uygular.
Bu işlevi SQL deyimlerini oluştururken kullanıyorsanız, kullanıcı girdisini bir SQL deyimi haline getirmek için PDO::quote() yerine PDO::prepare() ile değiştirgeleri ilişkilendirilmiş SQL deyimleri hazırlamanızı hararetle öneririz. Değiştirgelerle ilişkilendirimiş hazır deyimler taşınabilir olmaktan başka daha kullanışlı ve SQL zerkine bağışık olmanın yanında sorgunun derlenmiş hali hem sunucuda hem de istemcide bulunduğundan yorumlanan sorgulardan çok daha hızlıdır.
PDO sürücülerinin hepsi bu yöntemi gerçeklemez. (özellikle PDO_ODBC) Bu bakımdan hazır deyimleri kullanmaya hazır olmalısınız.
Değiştirgeler
-
dizge -
Öncelenecek dizge.
-
değiştirge_türü -
İkincil bir önceleme tarzına sahip sürücüler için ipucu olarak bir veri türü belirtilir.
PDO::PARAM_STRöntanımlıdır.
Dönen Değerler
Bir SQL deyiminde aktarılmak üzere teorik olarak güvenli kabul edilen bir
öncelenmiş dizge döndürür. Eğer sürücü bu tarz öncelemeyi desteklemiyorsa
FALSE döner.
Örnekler
Örnek 1 - Normal bir dizgeyi öncelemek
<?php
$conn = new PDO('sqlite:/home/lynn/music.sql3');
/*Basit dizge */
$string = 'Basit';
print "Öncelenmemiş dizge: $string\n";
print "Öncelenmiş dizge: " . $conn->quote($string) . "\n";
?>
Yukarıdaki örneğin çıktısı:
Öncelenmemiş dizge: Basit Öncelenmiş dizge: 'Basit'
Örnek 2 - Tehlikeli bir dizgeyi öncelemek
<?php
$conn = new PDO('sqlite:/home/lynn/music.sql3');
/* Tehlikeli dizge */
$string = 'Münasebetsiz \' dizge';
print "Öncelenmemiş dizge: $string\n";
print "Öncelenmiş dizge:" . $conn->quote($string) . "\n";
?>
Yukarıdaki örneğin çıktısı:
Öncelenmemiş dizge: Münasebetsiz ' dizge Öncelenmiş dizge: 'Münasebetsiz '' dizge'
Örnek 3 - Karmaşık bir dizgeyi öncelemek
<?php
$conn = new PDO('sqlite:/home/lynn/music.sql3');
/* Karmaşık dizge */
$string = "Co'mpl''ex \"st'\"ring";
print "Öncelenmemiş dizge: $string\n";
print "Öncelenmiş dizge:" . $conn->quote($string) . "\n";
?>
Yukarıdaki örneğin çıktısı:
Öncelenmemiş dizge: Co'mpl''ex "st'"ring Öncelenmiş dizge: 'Co''mpl''''ex "st''"ring'
Ayrıca Bakınız
- PDO::prepare() - Çalıştırılmak üzere bir deyimi hazırlar ve bir deyim nesnesi olarak döndürür
- PDOStatement::execute() - Bir hazır deyimi çalıştırır
One have to understand that string formatting has nothing to do with identifiers.
And thus string formatting should NEVER ever be used to format an identifier ( table of field name).
To quote an identifier, you have to format it as identifier, not as string.
To do so you have to
- Enclose identifier in backticks.
- Escape backticks inside by doubling them.
So, the code would be:
<?php
function quoteIdent($field) {
return "`".str_replace("`","``",$field)."`";
}
?>
this will make your identifier properly formatted and thus invulnerable to injection.
However, there is another possible attack vector - using dynamical identifiers in the query may give an outsider control over fields the aren't allowed to:
Say, a field user_role in the users table and a dynamically built INSERT query based on a $_POST array may allow a privilege escalation with easily forged $_POST array.
Or a select query which let a user to choose fields to display may reveal some sensitive information to attacker.
To prevent this kind of attack yet keep queries dynamic, one ought to use WHITELISTING approach.
Every dynamical identifier have to be checked against a hardcoded whitelist like this:
<?php
$allowed = array("name","price","qty");
$key = array_search($_GET['field'], $allowed));
if ($key == false) {
throw new Exception('Wrong field name');
}
$field = $db->quoteIdent($allowed[$key]);
$query = "SELECT $field FROM t"; //value is safe
?>
(Personally I wouldn't use a query like this, but that's just an example of using a dynamical identifier in the query).
And similar approach have to be used when filtering dynamical arrays for insert and update:
<?php
function filterArray($input,$allowed)
{
foreach(array_keys($input) as $key )
{
if ( !in_array($key,$allowed) )
{
unset($input[$key]);
}
}
return $input;
}
//used like this
$allowed = array('title','url','body','rating','term','type');
$data = $db->filterArray($_POST,$allowed);
// $data now contains allowed fields only
// and can be used to create INSERT or UPDATE query dynamically
?>
For those of you who do want to have null returned as NULL, it is fairly easy to add.
<?php
class Real_PDO extends PDO {
public function quote($value, $parameter_type = PDO::PARAM_STR ) {
if( is_null($value) ) {
return "NULL";
}
return parent::quote($value, $parameter_type);
}
}
?>
then all you have to do is change your creation of the PDO object to this new class, and you shouldn't have to change any other function calls. The nice thing about this method is that you can override any PDO function or add your own. IMO, PDO should natively handle this, as there is a difference in databases between NULL and an empty string(''), the quoting of numeric values is another issue altogether.
