Recently, I am working on a theme voting website. The customer understands some program things. There are special requirements for filtering some characters to prevent SQL injection. No special research has been conducted in this regard. Haha, I have carried forward a new post. Take the SQL anti-injection FUNCTION OF THE discuz Forum!
Copy codeThe Code is as follows: $ magic_quotes_gpc = get_magic_quotes_gpc ();
@ Extract (daddslashes ($ _ COOKIE ));
@ Extract (daddslashes ($ _ POST ));
@ Extract (daddslashes ($ _ GET ));
If (! $ Magic_quotes_gpc ){
$ _ FILES = daddslashes ($ _ FILES );
}
Function daddslashes ($ string, $ force = 0 ){
If (! $ GLOBALS ['Magic _ quotes_gpc '] | $ force ){
If (is_array ($ string )){
Foreach ($ string as $ key => $ val ){
$ String [$ key] = daddslashes ($ val, $ force );
}
} Else {
$ String = addslashes ($ string );
}
}
Return $ string;
}
You can enhance the following code to protect server security. It is very important for PHP to prevent SQL Injection security functions!Copy codeThe Code is as follows :/*
Function Name: inject_check ()
Function function: checks whether submitted values contain SQL Injection characters to prevent injection and protect server security.
Parameter: $ SQL _str: Submitted variable
Return Value: Return detection result, true or false
*/
Function inject_check ($ SQL _str ){
Return eregi ('select | insert | and | or | update | delete | \ '| \/\ * | \. \. \/| \. \/| union | into | load_file | outfile ', $ SQL _str); // Filter
}
/*
Function Name: verify_id ()
Function: checks whether the submitted ID class value is valid.
Parameter: $ id: Submitted ID value
Return Value: return the ID after processing.
*/
Function verify_id ($ id = null ){
If (! $ Id) {exit ('no submission parameter! ');} // Determines whether it is null.
Elseif (inject_check ($ id) {exit ('the submitted parameter is invalid! ');} // Injection judgment
Elseif (! Is_numeric ($ id) {exit ('the submitted parameter is invalid! ');} // Number judgment
$ Id = intval ($ id); // integer
Return $ id;
}
/*
Function Name: str_check ()
Function: Filter submitted strings.
Parameter: $ var: string to be processed
Return Value: returns the filtered string.
*/
Function str_check ($ str ){
If (! Get_magic_quotes_gpc () {// determines whether magic_quotes_gpc is enabled
$ Str = addslashes ($ str); // Filter
}
$ Str = str_replace ("_", "\ _", $ str); // filter '_'
$ Str = str_replace ("%", "\ %", $ str); // filter '%'
Return $ str;
}
/*
Function Name: post_check ()
Function: process submitted edits.
Parameter: $ post: content to be submitted
Return Value: $ post: The filtered content is returned.
*/
Function post_check ($ post ){
If (! Get_magic_quotes_gpc () {// determines whether magic_quotes_gpc is enabled.
$ Post = addslashes ($ post); // filter submitted data when magic_quotes_gpc is not enabled
}
$ Post = str_replace ("_", "\ _", $ post); // filter '_'
$ Post = str_replace ("%", "\ %", $ post); // filter '%'
$ Post = nl2br ($ post); // press enter to convert
$ Post = htmlspecialchars ($ post); // html tag Conversion
Return $ post;
}