Unified input data entry-php Tutorial

Source: Internet
Author: User
Unified entry class of input data
Http://blog.qita.in /? Posting = 494

  1. /**
  2. * Obtain unified input entries
  3. */
  4. Class cls_request
  5. {
  6. Private $ getdata; // Store get data
  7. Private $ postdata; // Store post data
  8. Private $ requestdata; // store the request data
  9. Private $ filedata; // stores file data
  10. Private $ cookiedata; // store cookies
  11. Static $ _ instance; // instance of this class
  12. Private function _ construct ()
  13. {
  14. $ This-> getdata = self: format_data ($ _ GET );
  15. $ This-> postdata = self: format_data ($ _ POST );
  16. $ This-> requestdata = array_merge ($ this-> getdata, $ this-> postdata );
  17. $ This-> cookiedata = self: format_data ($ _ COOKIE );
  18. $ This-> filedata = self: format_data ($ _ FILES );
  19. }
  20. /**
  21. * Class initialization method
  22. *
  23. * @ Return cls_request object
  24. */
  25. Public static function get_instance ()
  26. {
  27. If (! (Self: $ _ instance instanceof self ))
  28. {
  29. Self: $ _ instance = new self ();
  30. }
  31. Return self: $ _ instance;
  32. }
  33. /**
  34. * GET the value variable passed by GET.
  35. *
  36. * @ Param string $ key
  37. * @ Return int or big int
  38. */
  39. Publicfunction get_num ($ key)
  40. {
  41. If (! Isset ($ this-> getdata [$ key])
  42. {
  43. Return false;
  44. }
  45. Return $ this-> to_num ($ this-> getdata [$ key]);
  46. }
  47. /**
  48. * Get the numeric variable passed by POST
  49. *
  50. * @ Param string $ key
  51. * @ Return int or big int
  52. */
  53. Publicfunction post_num ($ key)
  54. {
  55. If (! Isset ($ this-> postdata [$ key])
  56. {
  57. Return false;
  58. }
  59. Return $ this-> to_num ($ this-> postdata [$ key]);
  60. }
  61. /**
  62. * Get the value variable passed in the REQUEST.
  63. *
  64. * @ Param string $ key
  65. * @ Return int or big int
  66. */
  67. Public function request_num ($ key)
  68. {
  69. If (! Isset ($ this-> requestdata [$ key])
  70. {
  71. Return false;
  72. }
  73. Return $ this-> to_num ($ this-> requestdata [$ key]);
  74. }
  75. /**
  76. * Obtain the value variable passed by the Cookie.
  77. *
  78. * @ Param string $ key
  79. * @ Return int or big int
  80. */
  81. Public function cookie_num ($ key)
  82. {
  83. If (! Isset ($ this-> cookiedata [$ key])
  84. {
  85. Return false;
  86. }
  87. Return $ this-> to_num ($ this-> cookiedata [$ key]);
  88. }
  89. /**
  90. * Get the variable value passed by Files.
  91. *
  92. * @ Param string $ key
  93. * @ Return array
  94. */
  95. Public function file_data ($ key)
  96. {
  97. Return $ this-> filedata [$ key];
  98. }
  99. /**
  100. * GET the string variable passed by GET.
  101. *
  102. * @ Param string $ key
  103. * @ Param boolen $ whether isfilter is filtered
  104. * @ Return string
  105. */
  106. Publicfunction get_string ($ key, $ isfilter = true)
  107. {
  108. If (! Isset ($ this-> getdata [$ key])
  109. {
  110. Return false;
  111. }
  112. If ($ isfilter)
  113. {
  114. Return $ this-> filter_string ($ this-> getdata [$ key]);
  115. }
  116. Else
  117. {
  118. Return $ this-> getdata [$ key];
  119. }
  120. }
  121. /**
  122. * Get the string variable passed by POST
  123. *
  124. * @ Param string $ key
  125. * @ Param boolen $ whether isfilter is filtered
  126. * @ Return string
  127. */
  128. Publicfunction post_string ($ key, $ isfilter = true)
  129. {
  130. If (! Isset ($ this-> postdata [$ key])
  131. {
  132. Return false;
  133. }
  134. If ($ isfilter)
  135. {
  136. Return $ this-> filter_string ($ this-> postdata [$ key]);
  137. }
  138. Else
  139. {
  140. Return $ this-> postdata [$ key];
  141. }
  142. }
  143. /**
  144. * Get the string variable passed in the REQUEST
  145. *
  146. * @ Param string $ key
  147. * @ Param boolen $ whether isfilter is filtered
  148. * @ Return string
  149. */
  150. Publicfunction request_string ($ key, $ isfilter = true)
  151. {
  152. If (! Isset ($ this-> requestdata [$ key])
  153. {
  154. Return false;
  155. }
  156. If ($ isfilter)
  157. {
  158. Return $ this-> filter_string ($ this-> requestdata [$ key]);
  159. }
  160. Else
  161. {
  162. Return $ this-> requestdata [$ key];
  163. }
  164. }
  165. /**
  166. * Get the string variable passed by the COOKIE
  167. *
  168. * @ Param string $ key
  169. * @ Param boolen $ whether isfilter is filtered
  170. * @ Return string
  171. */
  172. Public function cookie_string ($ key, $ isfilter = true)
  173. {
  174. If (! Isset ($ this-> cookiedata [$ key])
  175. {
  176. Return false;
  177. }
  178. If ($ isfilter)
  179. {
  180. Return $ this-> filter_string ($ this-> cookiedata [$ key]);
  181. }
  182. Else
  183. {
  184. Return $ this-> cookiedata [$ key];
  185. }
  186. }
  187. /**
  188. * Format the data and store the data.
  189. *
  190. * @ Param array $ data
  191. */
  192. Privatefunction format_data ($ data)
  193. {
  194. $ Result = array ();
  195. If (! Is_array ($ data ))
  196. {
  197. $ Data = array ();
  198. }
  199. While (list ($ key, $ value) = each ($ data ))
  200. {
  201. // Process data such as checkbox
  202. If (is_array ($ value ))
  203. {
  204. $ Result [$ key] = $ value;
  205. }
  206. Else // common data
  207. {
  208. $ Result [$ key] = trim ($ value );
  209. }
  210. }
  211. Return $ result;
  212. }
  213. /**
  214. * Convert to a number
  215. *
  216. * @ Param string $ num
  217. * @ Return int or big int or false
  218. */
  219. Private function to_num ($ num)
  220. {
  221. If (is_numeric ($ num ))
  222. {
  223. Return intval ($ num );
  224. }
  225. Else
  226. {
  227. Return false;
  228. }
  229. }
  230. /**
  231. * Convert and filter strings
  232. *
  233. * @ Param string/array $ data
  234. * @ Return string
  235. */
  236. Private function filter_string ($ data)
  237. {
  238. If ($ data = NULL)
  239. {
  240. Return false;
  241. }
  242. If (is_array ($ data ))
  243. {
  244. Foreach ($ data as $ k => $ v)
  245. {
  246. $ Data [$ k] = htmlspecialchars ($ v, ENT_QUOTES );
  247. }
  248. Return $ data;
  249. }
  250. Else
  251. {
  252. Return htmlspecialchars ($ data, ENT_QUOTES );
  253. }
  254. }
  255. }

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.