PHP function code for filtering input information safely

Source: Internet
Author: User

CopyCode The Code is as follows: // define constanets for input reading
Define ('input _ get', 0x0101 );
Define ('input _ Post', 0x0102 );
Define ('input _ GPC ', 0x0103 );

/**
* Read input value and convert it for internal use
* Performs stripslashes () and charset conversion if necessary
*
* @ Param string field name to read
* @ Param int source to get value from (GPC)
* @ Param Boolean allow HTML tags in Field Value
* @ Param string charset to convert
* @ Return string field value or null if not available
*/
Function get_input_value ($ fname, $ source, $ allow_html = false, $ charset = NULL ){
$ Value = NULL;

If ($ source = input_get & isset ($ _ Get [$ fname])
$ Value = $ _ Get [$ fname];
Else if ($ source = input_post & isset ($ _ post [$ fname])
$ Value = $ _ post [$ fname];
Else if ($ source = input_gpc ){
If (isset ($ _ post [$ fname])
$ Value = $ _ post [$ fname];
Else if (isset ($ _ Get [$ fname])
$ Value = $ _ Get [$ fname];
Else if (isset ($ _ cookie [$ fname])
$ Value = $ _ cookie [$ fname];
}

If (empty ($ value ))
Return $ value;

// Strip single quotes if magic_quotes_sybase is enabled
If (ini_get ('Magic _ quotes_sybase '))
$ Value = str_replace ("'' "," '", $ value );
// Strip slashes if magic_quotes Enabled
Else if (get_magic_quotes_gpc () | get_magic_quotes_runtime ())
$ Value = stripslashes ($ value );

// Remove HTML tags if not allowed
If (! $ Allow_html)
$ Value = strip_tags ($ value );

// Convert to internal charset
Return $ value;
}

usage: get_input_value ('_ uid', input_get)

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.