Input data unified entry class

WBOY
Release: 2016-07-25 09:01:57
Original
936 people have browsed it
Input data unified entry class http://blog.qita.in/?post=494
  1. /**
  2. * Get the unified entrance for input
  3. */
  4. class cls_request
  5. {
  6. private $getdata; //Storage get data
  7. private $postdata; //Storage post data
  8. private $requestdata; //Storage request data
  9. private $filedata; //Storage file data
  10. private $cookiedata; //Storage cookie
  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 numerical variable passed by GET
  35. *
  36. * @param string $key
  37. * @return int or big int
  38. */
  39. public function 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 numerical variable passed by POST
  49. *
  50. * @param string $key
  51. * @return int or big int
  52. */
  53. public function 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 numerical variable passed by 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. * Get the numerical variable passed by 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 $isfilter whether to filter
  104. * @return string
  105. */
  106. public function 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 $isfilter whether to filter
  126. * @return string
  127. */
  128. public function 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 by REQUEST
  145. *
  146. * @param string $key
  147. * @param boolen $isfilter whether to filter
  148. * @return string
  149. */
  150. public function 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 COOKIE
  167. *
  168. * @param string $key
  169. * @param boolen $isfilter whether to filter
  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 data and transfer data
  189. *
  190. * @param array $data
  191. */
  192. private function 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. //处理checkbox之类的数据
  202. if(is_array($value))
  203. {
  204. $result[$key]=$value;
  205. }
  206. else //普通数据
  207. {
  208. $result[$key] = trim($value);
  209. }
  210. }
  211. return $result;
  212. }
  213. /**
  214. * Convert to 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 filter string
  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. }
复制代码


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template