mysql_real_escape_string
(PHP 4 >= 4.3.0, PHP 5)
mysql_real_escape_string — 轉義 SQL 語句中使用的字串中的特殊字元,並考慮到連線的當前字符集
本擴充套件自 PHP 5.5.0 起已廢棄,並在自 PHP 7.0.0 開始被移除。應使用 MySQLi 或 PDO_MySQL 擴充套件來替換之。參見 MySQL:選擇 API 指南來獲取更多資訊。用以替代本函式的有:
說明
$unescaped_string
, resource $link_identifier
= NULL): string
本函式將
unescaped_string
中的特殊字元轉義,並計及連線的當前字符集,因此可以安全用於
mysql_query()。
mysql_real_escape_string() 呼叫mysql庫的函式
mysql_real_escape_string, 在以下字元前新增反斜槓:
\x00
, \n
,
\r
, \
, '
,
"
和 \x1a
.
爲了安全起見,在像MySQL傳送查詢前,必須呼叫這個函式(除了少數例外情況)。
安全提示: 預設字符集
The character set must be set either at the server level, or with the API function mysql_set_charset() for it to affect mysql_real_escape_string(). See the concepts section on character sets for more information.
參數
-
unescaped_string
-
The string that is to be escaped.
-
link_identifier
-
MySQL 連線。如不指定連線標識,則使用由 mysql_connect() 最近打開的連線。如果沒有找到該連線,會嘗試不帶參數呼叫 mysql_connect() 來建立。如沒有找到連線或無法建立連線,則會產生
E_WARNING
級別的錯誤。
返回值
Returns the escaped string, or false
on error.
錯誤/異常
Executing this function without a MySQL connection present will
also emit E_WARNING
level PHP errors. Only
execute this function with a valid MySQL connection present.
範例
示例 #1 mysql_real_escape_string() 例子
<?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));
?>
示例 #2 mysql_real_escape_string() 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 = ''"
示例 #3 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.
註釋
注意:
A MySQL connection is required before using mysql_real_escape_string() otherwise an error of level
E_WARNING
is generated, andfalse
is returned. Iflink_identifier
isn't defined, the last MySQL connection is used.
注意:
If magic_quotes_gpc is enabled, first apply stripslashes() to the data. Using this function on data which has already been escaped will escape the data twice.
注意:
If this function is not used to escape data, the query is vulnerable to SQL Injection Attacks.
注意: mysql_real_escape_string() does not escape
%
and_
. These are wildcards in MySQL if combined withLIKE
,GRANT
, orREVOKE
.
參見
- mysql_set_charset() - 設定客戶端的字符集
- mysql_client_encoding() - 返回字符集的名稱
- addslashes() - 使用反斜線引用字串
- stripslashes() - 反引用一個引用字串
- The magic_quotes_gpc directive
- The magic_quotes_runtime directive