首页 > 开发 > PHP > 正文

php输入数据统一类实例

2024-05-04 23:31:32
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了php输入数据统一类,实例分析了针对输入数据的各种转换技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php输入数据统一类。分享给大家供大家参考。具体如下:

 

 
  1. <?php 
  2. class cls_request{ 
  3. private $getdata;//存储get的数据 
  4. private $postdata;//存储post的数据 
  5. private $requestdata;//存储request的数据 
  6. private $filedata;//存储file的数据 
  7. private $cookiedata;//存储cooki 
  8. static $_instance;//本类的实例 
  9.  
  10. private function __construct(){ 
  11. $this->getdata = self::format_data($_GET); 
  12. $this->postdata = self::format_data($_POST); 
  13. $this->requestdata = array_merge($this->getdata,$this->postdata); 
  14. $this->cookiedata = self::format_data($_COOKIE); 
  15. $this->filedata = self::format_data($_FILES); 
  16. } 
  17. //类的初始化,返回cls_request对象 
  18. public static function get_instance(){ 
  19. if(!(self::$_instance instanceof self)){ 
  20. self::$_instance = new self(); 
  21. } 
  22. return self::$_instance; 
  23. } 
  24. //获取GET传递过来的数值变量 
  25. public function get_num($key){ 
  26. if(!isset($this->getdata[$key])){ 
  27. return false; 
  28. } 
  29. return $this->to_num($this->getdata[$key]); 
  30. } 
  31. //获取POST传递过来的数据变量 
  32. public function post_num($key){ 
  33. if(!isset($this->postdata[$key])){ 
  34. return false; 
  35. } 
  36. return $this->to_num($this->postdata[$key]); 
  37. } 
  38. //获取Request传递过来的数值变量 
  39. public function request_num($key){ 
  40. if(!isset($this->requestdata[$key])){ 
  41. return false; 
  42. } 
  43. return $this->to_num($this->requestdata[$key]); 
  44. } 
  45. //获取Cookie传递过来的数值变量 
  46. public function cookie_num($key){ 
  47. if(!isset($this->cookiedata[$key])){ 
  48. return false; 
  49. } 
  50. return $this->to_num($this->cookiedata[$key]); 
  51. } 
  52. //获取File传递过来的数值变量 
  53. public function filedata($key){ 
  54. return $this->filedata[$key];//返回数组 
  55. } 
  56. //获取GET传递过来的字符串变量 
  57. public function get_string($key,$isfilter=true){ 
  58. if(!isset($this->getdata[$key])){ 
  59. return false; 
  60. } 
  61. if($isfilter){ 
  62. return $this->filter_string($this->getdata[$key]); 
  63. }else{ 
  64. return $this->getdata[$key]; 
  65. } 
  66. } 
  67. //获取POST传递过来的字符串变量 
  68. public function post_string($key,$isfilter=true){ 
  69. if(!isset($this->postdata[$key])){ 
  70. return false; 
  71. } 
  72. if($isfilter){ 
  73. return $this->filter_string($this->postdata[$key]); 
  74. }else{ 
  75. return $this->postdata[$key]; 
  76. } 
  77. } 
  78. //获取Request传递过来的字符串变量 
  79. public function request_string($key,$isfilter=true){ 
  80. if(!isset($this->requestdata[$key])){ 
  81. return false; 
  82. } 
  83. if($isfilter){ 
  84. return $this->filter_string($this->requestdata[$key]); 
  85. }else{ 
  86. return $this->requestdata[$key]; 
  87. } 
  88. } 
  89. //获取Cookie传递过来的字符串变量 
  90. public function cookie_string($key,$isfilter=true){ 
  91. if(!isset($this->cookiedata[$key])){ 
  92. return false; 
  93. } 
  94. if($isfilter){ 
  95. return $this->filter_string($this->cookiedata[$key]); 
  96. }else{ 
  97. return $this->cookiedata[$key]; 
  98. } 
  99. } 
  100. //格式化数据 
  101. private function format_data($data){ 
  102. $result = array(); 
  103. if(!is_array($data)){ 
  104. $data = array(); 
  105. } 
  106. /* 
  107. *list()表示用数组的数值给变量赋值。只用于数字索引的数组, 
  108. *默认从0位开始,按顺序下去 
  109. *each() 
  110. */ 
  111. while(list($key,$value) = each($data)){//不太明白 
  112. //处理checkbox之类的数据 
  113. if(is_array($value)){ 
  114. $result[$key]=$value; 
  115. }else{//普通数据 
  116. $result[$key] = trim($value); 
  117. //删除字符串两端空白及其它预定义字符 
  118. } 
  119. } 
  120. } 
  121. //转化数字 
  122. private function to_num($num){ 
  123. if(is_numeric($num)){ 
  124. return intval($num);//将变量转为整数 
  125. }else{ 
  126. return false; 
  127. } 
  128. } 
  129. //过换过滤字符串 
  130. private function filter_string($data){ 
  131. if($data===null){ 
  132. return false; 
  133. } 
  134. if(is_array($data)){ 
  135. foreach($data as $k=>$v){ 
  136. $data[$k] = htmlspecialchars($v,ENT_QUOTES); 
  137. //把一些预定义字符转化为html实体 
  138. } 
  139. return $data; 
  140. }else{//普通字符串 
  141. return htmlspecialchars($data,ENT_QUOTES); 
  142. } 
  143. } 
  144. } 
  145. ?> 

希望本文所述对大家的php程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表