PHP general anti-injection V1.1

Source: Internet
Author: User

Programs may cause normal use of other programs and are trying to improve them!

Comments and corrections

<? Php
/* PHP universal injection prevention cross-site V1.1
################## Contact address ##################
Http://hi.baidu.com/menzhi007
################## Instructions ##################
Add require ("SQL _injection.php") at the top of your page ");
To prevent SQL injection and cross-site XSS vulnerabilities.
################## Defects and improvements ##################
There are still many defects in the program. I hope you can help improve it.
################## Reference and appreciation ##################
NeeaoASP SQL universal anti-injection program V3.0
Some code can be found in Discuz!
*/

Error_reporting (0 );
Define (MAGIC_QUOTES_GPC, get_magic_quotes_gpc ());

If (PHP_VERSION <4.1.0 ){
$ _ GET = & $ HTTP_GET_VARS;
$ _ POST = & $ HTTP_POST_VARS;
$ _ COOKIE = & $ HTTP_COOKIE_VARS;
$ _ SERVER = & $ HTTP_SERVER_VARS;
$ _ ENV = & $ HTTP_ENV_VARS;
$ _ FILES = & $ HTTP_POST_FILES;
}

Foreach (array (_ GET, _ POST, _ COOKIE, _ REQUEST) as $ _ request ){

Foreach ($ _ request as $ _ key => $ _ value ){
$ SQL _injection = "|; | and | (|) | exec | insert | select | delete | update | count | % | chr | mid | master | truncate | or | char | declare | benchmark | script | javascript | <|> ";
$ SQL _injection = explode ("|", $ SQL _injection );
Preg_match ("/[^ 0-9] */", $ _ key )? $ _ Key: $ _ key =;
Daddslashes ($ _ key );
Daddslashes ($ _ value );
$ _ Value = strtolower ($ _ value );
Foreach ($ SQL _injection as $ kill_key => $ kill_value ){
If (substr_count ($ _ value, $ kill_value)> 0 ){
Echo "<script> alert (Error, Do not enter illegal characters:". $ kill_value ."! Please Content menzhi007@163.com !); History. back (); </script> ";
Unset ($ _ request, $ _ key, $ _ value );
Exit ();
}
}
}

}

Function daddslashes ($ string ){

If (! MAGIC_QUOTES_GPC ){
If (is_array ($ string )){
Foreach ($ string as $ key => $ val ){
$ String [$ key] = daddslashes ($ val );
}
} Else {
$ String = addslashes ($ string );
}
}
$ String = preg_replace (/& (# (d {3, 5} | x [a-fA-F0-9] {4});)/, & \ 1, str_replace (array (&, ", <,>), array (&,", <,>), $ string ));

Return $ string;

}
?>
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.