Hjälpcentral

SQL injection, insertion

SQL injection is an attack where malicious code is passed to an SQL Server for execution. The attack can result in unauthorized access to confidential data or the destruction of critical data.

Before reading the methods below, realize that this should only be a concern for PHP developers and the like. If you use a database-driven program (e.g., WordPress, Joomla, OSCommerce), you need to upgrade your programs to the latest version.

Methods to prevent SQL Injection

Escaping

One way to prevent injections is to escape dangerous characters (i.e., backslash, apostrophe, and semicolon). In PHP, it is typical to run the input using the function mysql_real_escape_string before sending the SQL query. Example:

$Uname = mysql_real_escape_string($Uname);
$Pword = mysql_real_escape_string($Pword);
$query = "SELECT * FROM Users where UserName='$Uname' and Password='$Pword'";
mysql_query($query); 

Parameterized Statements

A parameterized query uses placeholders for the input, and the parameter values are supplied at execution time.

$params = array($Uname, $Pword);
$sql = 'INSERT INTO Users (UserName, Password) VALUES (?, ?)';
$query = sqlsrv_query($connection, $sql, $params); 

Advanced:

There are multiple choices in PHP version 5 and above for using parameterized statements; the PDO database layer is one. There are also vendor-specific methods; for example, MySQL 4.1 + used with the mysqli extension.

  • 0 användare blev hjälpta av detta svar
Hjälpte svaret dig?

Powered by WHMCompleteSolution