SQL dize tırnak ve komutları işlemek

2 Cevap php

Benim MySQL tablosunda, ben METİN türü bir sütun var. Benim HTML Form üzerinde, içine kullanıcı macunlar metin, böylece on "", '() içeren ve olabilir. Ben bu karakterleri metin var ve sorgu yürütme çökebilir eğer güvenli bir şekilde takın sorgu yürütmek için nasıl bilmek istiyorum.

PHP düzgün bunları işlemek nasıl?

2 Cevap

Bir prepared statement kullanın.

Prepared statements can help increase security by separating SQL logic from the data being supplied. This separation of logic and data can help prevent a very common type of vulnerability called an SQL injection attack. Normally when you are dealing with an ad hoc query, you need to be very careful when handling the data that you received from the user. This entails using functions that escape all of the necessary trouble characters, such as the single quote, double quote, and backslash characters. This is unnecessary when dealing with prepared statements. The separation of the data allows MySQL to automatically take into account these characters and they do not need to be escaped using any special function.

Hızlı bir örnek,

$db = new mysqli('localhost', 'username', 'password', 'db');
$stmt = $db->prepare("INSERT INTO mytable (text_column) VALUES (?)");
$stmt->bind_param("s", $mytext); // s = string, b = boolean, i = int, etc
$stmt->execute();
...

Eğer (ya PDO veya MySqli) Eğer kullanıcının giriş yalak MySql_Real_Escape_String() function geçmelidir. Hazırlanmış deyimleri kullanarak değilseniz Veya MySqli_Real_Escape_String() mysqli (ama hazır deyimleri) kullanıyorsanız.

Ben, ancak, hayatınızı çok daha kolay olacak gibi prepared statements kullanmak için tavsiye ediyorum ve ücretsiz SQL Injection koruma olsun.