Php anti-injection filter $ _ GET and $ _ POST parameters submitted by customers

Source: Internet
Author: User

The following is a program code to prevent SQL injection to php pages. For more information, see.

The following code filters the $ _ GET and $ _ POST parameters of php:

The Code is as follows: Copy code

/**
* Security Protection
*/
Function Add_S ($ array)
{
Foreach ($ array as $ key => $ value)
{
If (! Is_array ($ value ))
{
$ Value = get_magic_quotes_gpc ()? $ Value: addslashes ($ value );
$ Array [$ key] = filterHtml ($ value );
}
Else
{
Add_S ($ array [$ key]);
}
}
Return $ array;
}
Function glstr ($ var ){

If (is_array ($ var )){
Return Add_S ($ var );
}
Elseif (strlen ($ var )){
$ Var = get_magic_quotes_gpc ()? $ Var: addslashes ($ var );

$ Var = filterHtml ($ var );
}
Return $ var;
}
Function filterHtml ($ html)
{
$ Farr = array (
"/<! DOCTYPE ([^>] *?)> /Eis ",
"/<(/?) (Html | body | head | link | meta | base | input) ([^>] *?)> /Eis ",
"/<(Script | I? Frame | style | title | form )(.*?) </\ 1>/eis ",
"/(<[^>] *? S +) on [a-z] + s *? = ("| ') ([^ \ 2] *) \ 2 ([^>] *?>) /IsU ", // filter javascript on events
"/S +/", // filter unnecessary Spaces
);
$ Tarr = array (
"",
"",
"",
"\ 1 \ 4 ",
"",
);
$ Html = preg_replace ($ farr, $ tarr, $ html );
Return $ html;
}
If (sizeof ($ _ GET )){
Foreach ($ _ GET as $ key => $ value ){
$ _ GET [$ key] = glstr ($ value );//
}

}
If (sizeof ($ _ POST )){
Foreach ($ _ POST as $ key => $ value ){
$ _ POST [$ key] = glstr ($ value );//
}
}

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.