PHP anti-injection security implementation program code

Source: Internet
Author: User

I 've talked a lot about SQL anti-injection code in the past, but I still have to start with our server script. Below I will introduce some common methods for php anti-injection.

The most common example is

First, set magic_quotes_gpc to On and display_errors to Off. If the id type is set, we use intval () to convert it to an integer type, such as code:
$ Id = intval ($ id );

Now I will introduce the basic principles of data filtering submitted by php.

1) when submitting variables to the database, we must use addslashes () for filtering. For example, we can solve an addslashes () problem. In fact, when variable values are involved, the intval () function is also a good choice for string filtering.

2) Enable magic_quotes_gpc and magic_quotes_runtime in php. ini. Magic_quotes_gpc can change the quotation marks in get, post, and cookie to a slash. Magic_quotes_runtime can be used as a format for inbound and outbound data. In fact, this parameter is very popular as early as the previous injection was crazy.

3) when using system functions, you must use the escapeshellarg () and escapeshellcmd () parameters to filter them out, so that you can safely use the system functions.

4) for Cross-Site, strip_tags () and htmlspecialchars () parameters are both good, and user-submitted tags with html and php will be converted. For example, the angle brackets "<" are converted to harmless characters such as "<.

The Code is as follows: Copy code
$ New = htmlspecialchars ("<a href = 'test'> test </a>", ENT_QUOTES );
Strip_tags ($ text ,);

5) filter related functions, such as the previous include (), unlink, and fopen () functions. As long as you specify the variables for the operation you want to perform or strictly filter the relevant characters, I think this will be impeccable.

2. Simple PHP Data Filtering

1) warehouse receiving: trim ($ str), addslashes ($ str)
2) outbound: stripslashes ($ str)
3) display: htmlspecialchars (nl2br ($ str ))

Share an instance

Specific Code:

The Code is as follows: Copy code

<? Php
// Security. php
/**
* @ Author zhengwei
* @ Copyright 2007
*/

/*
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 Author: heiyeluren
*/
Function inject_check ($ SQL _str ){
Return eregi ('select | insert | 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 Author: heiyeluren
*/
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 Author: heiyeluren
*/
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 Author: heiyeluren
*/
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;
}


Foreach ($ _ POST as $ post_key => $ post_var)
{
If (is_numeric ($ post_var )){
$ Post [strtolower ($ post_key)] = get_int ($ post_var );
} Else {
$ Post [strtolower ($ post_key)] = get_str ($ post_var );
}
}

/* Filter function */
// Integer Filter Function
Function get_int ($ number)
{
Return intval ($ number );
}
// String Filter Function
Function get_str ($ string)
{
If (! Get_magic_quotes_gpc ()){
Return addslashes ($ string );
}
Return $ string;
}

?>

In some cms, I will see

Foreach ($ HTTP_POST_VARS as $ key => $ value ){
$ ArrPostAndGet [] = $ value;
}
Foreach ($ HTTP_GET_VARS as $ key => $ value ){
$ ArrPostAndGet [] = $ value;
}


 

This code then loads this function on all pages, so as to filter out the code and find that there may be problems when uploading files.

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.