How to implement a PHP framework series article "5" Secure processing input

Source: Internet
Author: User
Tags php framework

All external input parameters should be checked for legitimacy.

Improper processing of input data can lead to vulnerabilities such as SQL injection.

The framework provides a series of functions to take values from $_request

Requestint

RequestString

Requestfloat

Requestbool

PS: Note that variable types in $_request may be arrays

If the request is i[]=1, then the value of $_request[' I '] is array (1)

When doing the calibration to consider comprehensive to prevent PHP warning information disclosure

In addition, we introduce the data check in KV JSON format.

Sometimes in order to retain some extensibility in the project, the JSON-formatted data is used, and how is this data validated?

Check key value form {k1:v1, K2:v2, K3:v3 ...} JSON data, each pair of kv can be verified

Requestkvjson

Partial implementation Code

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 //校验整数,失败返回$defaultfunctioncheckInt($var$default= 0) {    returnis_numeric($var) ? intval($var, (strncasecmp($var‘0x‘, 2) == 0 || strncasecmp($var‘-0x‘, 3) == 0) ? 16 : 10) : $default;}//校验字符串 $check为正则表达式functioncheckString($var$check‘‘$default ‘‘) {    if(!is_string($var)) {        if(is_numeric($var)) {            $var= (string)$var;        }        else{            return$default;        }    }    if($check) {         return(preg_match($check$var$ret) ? $ret[1] : $default);    }    return$var;}/*    校验kv json,    如果想要一个这样的数据{id:1, ‘type‘:‘single_text‘, ‘required‘: true, ‘desc‘:‘this is a text‘}    那么$desc可以这样写    array(   array(‘id‘, ‘Int‘),   array(‘type‘, ‘string‘, PATTERN_NORMAL_STRING),   array(‘required‘, ‘Bool‘, false),   array(‘desc‘, ‘string‘, PATTERN_NORMAL_STRING),))*/functioncheckKvJson($var$descarray()) {    if(is_string($var)) {        $var= json_decode($var, true);    }    if(!$var|| !is_array($var)) {        returnarray();    }    if($desc)    foreach($descas $d) {        if(!isset($var[$d[0]])) {            returnarray();        }         $psarray_slice($d, 2);        array_unshift($ps$var[$d[0]]);        $var[$d[0]] = call_user_func_array(‘check‘.$d[1], $ps);        if($var[$d[0]] === false && strcasecmp($d[1], ‘Bool‘)) {            returnarray();        }    }     return$var;}

How to implement a PHP framework series article "5" Secure processing input

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.