When your data contains some \"'
This type of character is useful when it is written to the database and is not filtered out. It will automatically add \ before these characters, such
China \ "Haha"
China \ \ "Haha \"
You can use set_maginc_quotes_runtime (0) to disable it. Of course, you can also directly set it in php. ini.
Get_magic_quotes_runtime () gets the value of the PHP environment variable magic_quotes_runtime.
Magic_quotes_gpc is on, which automatically runs addslashes () on all GET, POST, and COOKIE data (). Do not use addslashes () for strings that have been escaped by magic_quotes_gpc, because this causes double-layer escape. In this case, you can use the get_magic_quotes_gpc () function for detection.
Different
Set_magic_quotes_runtime () allows programmers to dynamically enable or disable magic_quotes_runtime in code,
Set_magic_quotes_runtime (1) indicates enabled, and set_magic_quotes_runtime (0) indicates disabled. When set_magic_quotes_runtime (1), the text read from the database or through functions such as fread will be automatically added to 'and \ with a backslash \ escape to prevent overflow. This is useful when transferring database data. However, in general, it should be disabled. Otherwise, the single quotation marks, double quotation marks, and backslash (\) of data read from the database will be added, leading to abnormal display. For example, Discuz and PHPWind Add a set_magic_quotes_runtime (0) to the header of the public file, and forcibly disable magic_quotes_runtime.
Magic_quotes_gpc
Scope of application: WEB client server;
Time: Request start, for example, when the script is running.
Magic_quotes_runtime
Scope: obtained from the data read from the file, the exec () execution result, or the SQL query;
Time: each time the script accesses the data generated in the running state.
So
The value of magic_quotes_gpc will affect the data obtained through Get/Post/Cookies,
The value of magic_quotes_runtime will affect the data read from the file or the data queried from the database,
Magic_quotes_gpc is used to escape data transmitted through GET, POST, and COOKIE. Generally, it must be escaped before data is imported into the database,
Magic_quotes_gpc cannot be dynamically enabled or disabled in the code. You need to go to php. ini and set magic_quotes_gpc to on or off,
In the code, you can use get_magic_quotes_gpc to obtain the status of magic_quotes_gpc.
When magic_quotes_gpc is off, you must manually perform addslashes on the data. The Code is as follows:
Copy codeThe Code is as follows:
If (! Get_magic_quotes_gpc ()){
New_addslashes ($ _ GET );
New_addslashes ($ _ POST );
New_addslashes ($ _ COOKIE );
}
Function new_addslashes ($ string ){
If (is_array ($ string )){
Foreach ($ string as $ key => $ value ){
$ String [$ key] = new_addslashes ($ value );
}
} Else {
$ String = addslashes ($ string );
}
Return $ string;
}
Another example:
Copy codeThe Code is as follows:
$ Data1 = $ _ POST ['aaa'];
$ Data2 = implode(file('1.txt '));
If (get_magic_quotes_gpc ()){
// Write data $ data1 directly to the database
} Else {
$ Data1 = addslashes ($ data1 );
// Write data $ data1 to the database
}
If (get_magic_quotes_runtime ()){
// Write data $ data2 directly to the database
// The data read from the database must be output after a stripslashes () operation.
} Else {
$ Data2 = addslashes ($ data2 );
// Write data $ data2 to the database
// Directly output the data read from the database
}
++ ++
Experience summary:
1. for GPC, whether magic_quotes_gpc (magic_quotes_gpc = On in php. ini) is enabled or not, magic_quotes_gpc is enabled to escape get, post, and cookie content. The procedure is as follows:
(From uchome System)
Copy codeThe Code is as follows:
Function saddslashes ($ string ){
If (is_array ($ string )){
Foreach ($ string as $ key => $ val ){
$ String [$ key] = saddslashes ($ val );
}
} Else {
$ String = addslashes ($ string );
}
Return $ string;
}
// GPC Filtering
$ Magic_quote = get_magic_quotes_gpc ();
If (empty ($ magic_quote )){
$ _ GET = saddslashes ($ _ GET );
$ _ POST = saddslashes ($ _ POST );
}
// COOKIE, escape the cookie value
$ Prelength = strlen ($ _ SC ['cookiepre']);
Foreach ($ _ COOKIE as $ key => $ val ){
If (substr ($ key, 0, $ prelength) == _ SC ['cookiepre']) {
$ _ SCOOKIE [(substr ($ key, $ prelength)] = empty ($ magic_quote )? Saddslashes ($ val): $ val;
}
}
2. For magic_quotes_runtime, we disable it in a unified manner, that is, set_magic_quotes_runtime (0). Do not allow the single quotation marks, double quotation marks, and backslash of data read from the database to be automatically added \. In this way, perform the following operations on the database: before adding data to the database, we manually perform the addslashes () operation on the data, and when the data is retrieved from the database, perform the opposite operation, that is, stripslashes ().
3. Keep raw data for the content to be serialized, that is, remove the Escape Character, stripslashes (), and save the serialized content to the database. (Note, the serialized content does not contain single quotation marks ('), double quotation marks ("), or backslash (\). The example is as follows:
$ Feedarr ['body _ data'] = serialize (stripslashes ($ body_data ));
++ ++
Is Function set_magic_quotes_runtime () is deprecated?
After installing PHPCMS, The Deprecated: Function set_magic_quotes_runtime () is deprecated error occurs. After checking the network and data, the set_magic_quotes_runtime () Function is removed after PHP5.3 and PHP6.0.
I can use the following alternative solution:
View sourceprint?
@ Set_magic_quotes_runtime (0 );
Or
View sourceprint?
Ini_set ("magic_quotes_runtime", 0 );
Or
View sourceprint?
If (phpversion () <'5. 3.0 '){
Set_magic_quotes_runtime (0 );
}