(PHP 4 >= 4.3.0, PHP 5)
mysql_real_escape_string — 轉(zhuǎn)義 SQL 語(yǔ)句中使用的字符串中的特殊字符,并考慮到連接的當(dāng)前字符集
本擴(kuò)展自 PHP 5.5.0 起已廢棄,并在自 PHP 7.0.0 開始被移除。應(yīng)使用 MySQLi 或 PDO_MySQL 擴(kuò)展來替換之。參見 MySQL:選擇 API 指南來獲取更多信息。用以替代本函數(shù)的有:
$unescaped_string
, resource $link_identifier
= NULL): string
本函數(shù)將
unescaped_string
中的特殊字符轉(zhuǎn)義,并計(jì)及連接的當(dāng)前字符集,因此可以安全用于
mysql_query()。
mysql_real_escape_string() 調(diào)用mysql庫(kù)的函數(shù)
mysql_real_escape_string, 在以下字符前添加反斜杠:
\x00
, \n
,
\r
, \
, '
,
"
和 \x1a
.
為了安全起見,在像MySQL傳送查詢前,必須調(diào)用這個(gè)函數(shù)(除了少數(shù)例外情況)。
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
連接。如不指定連接標(biāo)識(shí),則使用由 mysql_connect()
最近打開的連接。如果沒有找到該連接,會(huì)嘗試不帶參數(shù)調(diào)用
mysql_connect()
來創(chuàng)建。如沒有找到連接或無法建立連接,則會(huì)生成
E_WARNING
級(jí)別的錯(cuò)誤。
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
.