// GPC filter, automatically escape special characters in $ _ GET, $ _ POST, and $ _ cookies to prevent SQL injection attacks
$ _ GET = saddslashes ($ _ GET );
$ _ POST = saddslashes ($ _ POST );
Copy codeThe Code is as follows: The following is an example of daddslashes () and saddslashes (): saddslashes function daddslashes ($ string, $ force = 0, $ strip = FALSE ){
// Whether to forcibly remove strings or Arrays
// If the magic reference is not enabled or $ force is not 0
If (! MAGIC_QUOTES_GPC | $ force ){
If (is_array ($ string) {// if it is an array, this function is executed cyclically.
Foreach ($ string as $ key => $ val ){
$ String [$ key] = daddslashes ($ val, $ force );
}
} Else {
// If magic reference is enabled or $ force is 0
// The following is a ternary operator. If $ strip is true, execute stripslashes to remove the backslash character and then execute addslashes.
// $ Strip is true, that is, remove the backslash character and escape it as $ _ GET, $ _ POST, $ _ COOKIE and $ _ REQUEST arrays contain the values of the first three arrays.
// Here, why do we remove the backslash and escape the $ string first, because sometimes $ string may have two backslashes, and stripslashes filters out unnecessary backslashes?
$ String = addslashes ($ strip? Stripslashes ($ string): $ string );
}
}
Return $ string;
} Eg: saddslashes function saddslashes ($ string) {if (! MAGIC_QUOTES_GPC ){
If (is_array ($ string) {// if the escape is an array, recursively escape the values in the array
Foreach ($ string as $ key => $ val ){
$ String [$ key] = saddslashes ($ val );
}
} Else {
$ String = addslashes ($ string); // escape single quotation marks ('), double quotation marks ("), backslash (\), and NUL (NULL character)
}
Return $ string;
} Else {
Return $ string;
}
Mainly:
Saddslashes can escape each data.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;
}