Ist die Verhinderung von XSS und SQL-Injection so einfach wie dieses

Frage: verhindert XSS (cross-site-scripting) so einfach mit strip_tags auf alle gespeicherten input-Felder und laufen htmlspecialchars auf jede angezeigte Ausgabe ... und verhindert SQL-Injection mittels PHP PDO prepared statements?

Hier ein Beispiel:

//INPUT: Input a persons favorite color and save to database
//this should prevent SQL injection ( by using prepared statement)
//and help prevent XSS  (by using strip_tags)
$sql = 'INSERT INTO TABLE favorite (person_name, color) VALUES (?,?)';
$sth = $conn->prepare($sql);
$sth->execute(array(strip_tags($_POST['person_name']), strip_tags($_POST['color'])));


//OUTPUT: Output a persons favorite color from the database
//this should prevent XSS (by using htmlspecialchars) when displaying
$sql = 'SELECT color FROM favorite WHERE person_name = ?';
$sth = $conn->prepare($sql);
$sth->execute(array(strip_tags($_POST['person_name'])));
$sth->setFetchMode(PDO::FETCH_BOTH);
while($color = $sth->fetch()){
  echo htmlspecialchars($color, ENT_QUOTES, 'UTF-8');
}
  • Nicht ganz fangen Sie - wollen Sie an mysql_query() der $save_to_database Wert? Dann müssen Sie noch nennen mysql_real_escape_string() bevor Sie senden Sie Ihre Anfrage, sonst sind Sie nicht geschützt vor SQL-Injektionen (die nicht XSS, wieder)
  • Sind Sie sich da sicher? Er ist mit prepared statements.
InformationsquelleAutor TimTim | 2010-01-03
Schreibe einen Kommentar