Php input data unified class instance _ PHP Tutorial

Source: Internet
Author: User
Php unified input data Class instance. This article mainly introduces the php unified input data Class. the instance analyzes various conversion techniques for input data, which has some reference value, A friend who needs a unified php input data Class instance

This article mainly introduces the unified class of php input data. The example analyzes various conversion techniques for input data and has some reference value. For more information, see

This example describes the unified class of php input data. Share it with you for your reference. The details are as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

Class cls_request {

Private $ getdata; // Store get data

Private $ postdata; // Store post data

Private $ requestdata; // store the request data

Private $ filedata; // stores file data

Private $ cookiedata; // store cooki

Static $ _ instance; // instance of this class

Private function _ construct (){

$ This-> getdata = self: format_data ($ _ GET );

$ This-> postdata = self: format_data ($ _ POST );

$ This-> requestdata = array_merge ($ this-> getdata, $ this-> postdata );

$ This-> cookiedata = self: format_data ($ _ COOKIE );

$ This-> filedata = self: format_data ($ _ FILES );

}

// Class initialization, return the cls_request object

Public static function get_instance (){

If (! (Self: $ _ instance instanceof self )){

Self: $ _ instance = new self ();

}

Return self: $ _ instance;

}

// Obtain the value variable passed by GET.

Public function get_num ($ key ){

If (! Isset ($ this-> getdata [$ key]) {

Return false;

}

Return $ this-> to_num ($ this-> getdata [$ key]);

}

// Obtain the data variable passed by POST

Public function post_num ($ key ){

If (! Isset ($ this-> postdata [$ key]) {

Return false;

}

Return $ this-> to_num ($ this-> postdata [$ key]);

}

// Obtain the value variable passed by the Request

Public function request_num ($ key ){

If (! Isset ($ this-> requestdata [$ key]) {

Return false;

}

Return $ this-> to_num ($ this-> requestdata [$ key]);

}

// Obtain the value variable passed by the Cookie

Public function cookie_num ($ key ){

If (! Isset ($ this-> cookiedata [$ key]) {

Return false;

}

Return $ this-> to_num ($ this-> cookiedata [$ key]);

}

// Obtain the numeric variable passed by File

Public function filedata ($ key ){

Return $ this-> filedata [$ key]; // returns an array

}

// GET the string variable passed by GET

Public function get_string ($ key, $ isfilter = true ){

If (! Isset ($ this-> getdata [$ key]) {

Return false;

}

If ($ isfilter ){

Return $ this-> filter_string ($ this-> getdata [$ key]);

} Else {

Return $ this-> getdata [$ key];

}

}

// Obtain the string variable passed by POST

Public function post_string ($ key, $ isfilter = true ){

If (! Isset ($ this-> postdata [$ key]) {

Return false;

}

If ($ isfilter ){

Return $ this-> filter_string ($ this-> postdata [$ key]);

} Else {

Return $ this-> postdata [$ key];

}

}

// Obtain the string variable passed by the Request

Public function request_string ($ key, $ isfilter = true ){

If (! Isset ($ this-> requestdata [$ key]) {

Return false;

}

If ($ isfilter ){

Return $ this-> filter_string ($ this-> requestdata [$ key]);

} Else {

Return $ this-> requestdata [$ key];

}

}

// Obtain the string variable passed by the Cookie

Public function cookie_string ($ key, $ isfilter = true ){

If (! Isset ($ this-> cookiedata [$ key]) {

Return false;

}

If ($ isfilter ){

Return $ this-> filter_string ($ this-> cookiedata [$ key]);

} Else {

Return $ this-> cookiedata [$ key];

}

}

// Format the data

Private function format_data ($ data ){

$ Result = array ();

If (! Is_array ($ data )){

$ Data = array ();

}

/*

* List () indicates that the variable is assigned a value using the array value. Used only for numeric index arrays,

* Starts from 0 by default and goes down in order

* Each ()

*/

While (list ($ key, $ value) = each ($ data) {// not quite clear

// Process data such as checkbox

If (is_array ($ value )){

$ Result [$ key] = $ value;

} Else {// common data

$ Result [$ key] = trim ($ value );

// Delete white spaces and other predefined characters at both ends of the string

}

}

}

// Convert numbers

Private function to_num ($ num ){

If (is_numeric ($ num )){

Return intval ($ num); // Convert the variable to an integer

} Else {

Return false;

}

}

// Exchange filter string

Private function filter_string ($ data ){

If ($ data = null ){

Return false;

}

If (is_array ($ data )){

Foreach ($ data as $ k => $ v ){

$ Data [$ k] = htmlspecialchars ($ v, ENT_QUOTES );

// Convert some predefined characters into html objects

}

Return $ data;

} Else {// normal string

Return htmlspecialchars ($ data, ENT_QUOTES );

}

}

}

?>

I hope this article will help you with php programming.

This article introduces the unified class of php input data. The example analyzes various conversion techniques for input data and has some reference 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.