Let's start with a function that's just introduced in WordPress 3.6 recently.
/**
* Add slashes to a string or array of strings.
* This should is used when preparing the data for the
core API that expects slashed data.
* This should is used to escape data going directly into SQL query.
* *
@since 3.6.0
* *
@param string|array $value string or array of strings to slash.
* @return String|array slashed $value
/function Wp_slash ($value) {
if (Is_array ($value)) {
for Each ($value as $k => $v) {
if (Is_array ($v)) {
$value [$k] = Wp_slash ($v);
} else {
$value [ $k] = addslashes ($v);}}}
else {
$value = addslashes ($value);
}
return $value;
}
First describe 1 PHP built-in functions: GET_MAGIC_QUOTES_GPC ()
The function is to get the value of the MAGIC_QUOTES_GPC option in the php.ini setting.
The MAGIC_QUOTES_GPC option, if the value is the on,php parser, automatically adds the escape character "\" for post, get, and cookie data to ensure that the data does not cause a program, especially a fatal error caused by a special character in a database statement.
When opened, single quotes ('), double quotes ("), backslashes (\) and NUL (NULL characters) are added to the backslash, otherwise manual processing will be used to addslashes ()
Returns 1 if the MAGIC_QUOTES_GPC value is on, otherwise returns 0
The Addslashes () function adds a backslash before the specified predefined character. Which is the character listed above
However, the GET_MAGIC_QUOTES_GPC () built-in function is canceled above PHP5.4, so that all input is filtered in order to avoid future errors:
if (!function_exists (GET_MAGIC_QUOTES_GPC) | | |!GET_MAGIC_QUOTES_GPC ())) {
foreach (' _cookie ', ' _post ', ' _ Get ') as $v) {
foreach ($ $v as $kk => $vv) {
$kk {0}!= ' _ ' && $ $v [$KK] = addslashes ($VV);
}
}
}
When dealing with MySQL and get, post data, it is often necessary to escape the quotation marks of the data.
There are three settings in PHP that enable automatic "(single quotes)," (double quotes), \ (backslashes), and NULL characters to be turned.
PHP is called Magic quotes, these three settings are
Magic_quotes_gpc
Affects HTTP request data (Get,post and cookies). Cannot be changed at run time. In PHP, the default value is on.
When this is turned on, the data passed through the Get,post,cookie is automatically escaped.
such as Test.php?id=abc ' de ' F
echo $_get[' id ']; # will get abc\ ' de\ ' F
Magic_quotes_gpc=on; This is turned on, which has no effect on writing to the database, such as the $_get[' ID ' written to the database, which is still abc ' de ' F,
Conversely, if magic_quotes_gpc=off; If you want to enclose the characters in quotation marks (whether single or double quotes), writing to MySQL directly becomes a blank
However, if you write it to a document, not MySQL. Then it will be abc\ ' de\ ' F
Magic_quotes_runtime
If opened, most of the data returned from external sources, including from the database and text files, will be escaped by backslashes. This option can be changed at run time, and the default value in PHP is off.
Magic_quotes_sybase
If opened, the single quotation marks are used to escape instead of the backslash. This option will completely overwrite MAGIC_QUOTES_GPC. If you open two options at the same time, the single quote will be escaped. Double quotes, backslashes, and NULL characters are not escaped.
My form content originally is:
Countermeasure One: Modify php.ini file (Modify PHP.ini This method will not say, we can Google)
Game Two: Cancel the escape.
Step one: Find the data you submitted, such as $_post[' content ', and change it to $content=stripslashes ($_post[' content ').
The second step: later in the use of $post[' content ' of places are replaced by $content
Step three: Submit to the database, database storage is still normal: Read it again.
(this should know how to solve it? Or I'm going to be more verbose.
Fourth step: Read the contents of the database again with Stripslashes () filter.
Stripslashes () This function deletes the backslash that was added by the addslashes () function. Used to clean up data retrieved from a database or HTML form
(
In the PHP page, if you don't want the following to happen:
Single quotes are escaped to \ '
Double quotes are escaped to \ "
Then you can set the following to prevent:
Set in php.ini: MAGIC_QUOTES_GPC = off)
Summarized as follows:
1. For the magic_quotes_gpc=on situation ,
We can not make string data for input and output databases
Addslashes () and Stripslashes (), the data is also displayed correctly.
If you addslashes () the input data at this time,
Then you must use Stripslashes () in the output to remove the extra backslash.
2. For the Magic_quotes_gpc=off situation
You must use Addslashes () to process the input data, but you do not need to use stripslashes () to format the output
Because Addslashes () did not write the backslash to the database, it only helped MySQL complete the execution of the SQL statement.