mysql_real_escape_string
mysql_real_escape_string
Escapes special characters in a string for use in an SQL statement
Waarschuwing: >
mysqli_real_escape_string``PDO::quote
string **mysql_real_escape_string** string $unescaped_string resource $link_identifier
Escapes special characters in the ,
taking into account the current character set of the connection so that it
is safe to place it in a . If binary data
is to be inserted, this function must be used.
unescaped_string``mysql_query
calls MySQL's library function
mysql_real_escape_string, which prepends backslashes to the following characters:
, ,
, , ,
and .
mysql_real_escape_string``\x00``\n``\r``\``'``"``\x1a
This function must always (with few exceptions) be used to make data safe before sending a query to MySQL.
Let op: > ### Security: the default character set
The character set must be set either at the server level, or with the API function for it to affect . See the concepts section on for more information.
mysql_set_charset``mysql_real_escape_stringcharacter sets
unescaped_stringThe string that is to be escaped.
Returns the escaped string, or false on error.
Executing this function without a MySQL connection present will
also emit level PHP errors. Only
execute this function with a valid MySQL connection present.
E_WARNING
Voorbeeld: Simple example
<?php
// Connect
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
OR die(mysql_error());
// Query
$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
mysql_real_escape_string($user),
mysql_real_escape_string($password));
?>
Voorbeeld: requires a connection example
This example demonstrates what happens if a MySQL connection is not present when calling this function.
<?php
// We have not connected to MySQL
$lastname = "O'Reilly";
$_lastname = mysql_real_escape_string($lastname);
$query = "SELECT * FROM actors WHERE last_name = '$_lastname'";
var_dump($_lastname);
var_dump($query);
?>
Warning: mysql_real_escape_string(): No such file or directory in /this/test/script.php on line 5
Warning: mysql_real_escape_string(): A link to the server could not be established in /this/test/script.php on line 5
bool(false)
string(41) "SELECT * FROM actors WHERE last_name = ''"
Voorbeeld: An example SQL Injection Attack
<?php
// We didn't check $_POST['password'], it could be anything the user wanted! For example:
$_POST['username'] = 'aidan';
$_POST['password'] = "' OR ''='";
// Query database to check if there are any matching users
$query = "SELECT * FROM users WHERE user='{$_POST['username']}' AND password='{$_POST['password']}'";
mysql_query($query);
// This means the query sent to MySQL would be:
echo $query;
?>
The query sent to MySQL:
SELECT * FROM users WHERE user='aidan' AND password='' OR ''=''
This would allow anyone to log in without a valid password.
Opmerking: > A MySQL connection is required before using otherwise an error of level is generated, and false is returned. If isn't defined, the last MySQL connection is used.
mysql_real_escape_string``E_WARNING``link_identifier
Opmerking: > If this function is not used to escape data, the query is vulnerable to . SQL Injection Attacks
Opmerking: > does not escape and . These are wildcards in MySQL if combined with , , or .
mysql_real_escape_string``%``_``LIKE``GRANT``REVOKE
mysql_set_charset``mysql_client_encoding