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

Source: Internet
Author: User
How to implement a PHP framework series article "5" Secure processing input

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

//校验整数,失败返回$default

function checkInt($var, $default = 0) {

return is_numeric($var) ? intval($var, (strncasecmp($var, '0x', 2) == 0 || strncasecmp($var, '-0x', 3) == 0) ? 16 : 10) : $default;

}

//校验字符串 $check为正则表达式

function checkString($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),

))

*/

function checkKvJson($var, $desc = array()) {

if(is_string($var)) {

$var = json_decode($var, true);

}

if(!$var || !is_array($var)) {

return array();

}

if($desc)

foreach($desc as $d) {

if(!isset($var[$d[0]])) {

return array();

}

$ps = array_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')) {

return array();

}

}

return $var;

}

  • 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.