国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 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. //類的初始化,返回cls_request對象 
  17. public static function get_instance(){ 
  18. if(!(self::$_instance instanceof self)){ 
  19. self::$_instance = new self(); 
  20. return self::$_instance
  21. //獲取GET傳遞過來的數值變量 
  22. public function get_num($key){ 
  23. if(!isset($this->getdata[$key])){ 
  24. return false; 
  25. return $this->to_num($this->getdata[$key]); 
  26. //獲取POST傳遞過來的數據變量 
  27. public function post_num($key){ 
  28. if(!isset($this->postdata[$key])){ 
  29. return false; 
  30. return $this->to_num($this->postdata[$key]); 
  31. //獲取Request傳遞過來的數值變量 
  32. public function request_num($key){ 
  33. if(!isset($this->requestdata[$key])){ 
  34. return false; 
  35. return $this->to_num($this->requestdata[$key]); 
  36. //獲取Cookie傳遞過來的數值變量 
  37. public function cookie_num($key){ 
  38. if(!isset($this->cookiedata[$key])){ 
  39. return false; 
  40. return $this->to_num($this->cookiedata[$key]); 
  41. //獲取File傳遞過來的數值變量 
  42. public function filedata($key){ 
  43. return $this->filedata[$key];//返回數組 
  44. //獲取GET傳遞過來的字符串變量 
  45. public function get_string($key,$isfilter=true){ 
  46. if(!isset($this->getdata[$key])){ 
  47. return false; 
  48. if($isfilter){ 
  49. return $this->filter_string($this->getdata[$key]); 
  50. }else
  51. return $this->getdata[$key]; 
  52. //獲取POST傳遞過來的字符串變量 
  53. public function post_string($key,$isfilter=true){ 
  54. if(!isset($this->postdata[$key])){ 
  55. return false; 
  56. if($isfilter){ 
  57. return $this->filter_string($this->postdata[$key]); 
  58. }else
  59. return $this->postdata[$key]; 
  60. //獲取Request傳遞過來的字符串變量 
  61. public function request_string($key,$isfilter=true){ 
  62. if(!isset($this->requestdata[$key])){ 
  63. return false; 
  64. if($isfilter){ 
  65. return $this->filter_string($this->requestdata[$key]); 
  66. }else
  67. return $this->requestdata[$key]; 
  68. //獲取Cookie傳遞過來的字符串變量 
  69. public function cookie_string($key,$isfilter=true){ 
  70. if(!isset($this->cookiedata[$key])){ 
  71. return false; 
  72. if($isfilter){ 
  73. return $this->filter_string($this->cookiedata[$key]); 
  74. }else
  75. return $this->cookiedata[$key]; 
  76. //格式化數據 
  77. private function format_data($data){ 
  78. $result = array(); 
  79. if(!is_array($data)){ 
  80. $data = array(); 
  81. /* 
  82. *list()表示用數組的數值給變量賦值。只用于數字索引的數組, 
  83. *默認從0位開始,按順序下去 
  84. *each() 
  85. */ 
  86. while(list($key,$value) = each($data)){//不太明白 
  87. //處理checkbox之類的數據 
  88. if(is_array($value)){ 
  89. $result[$key]=$value
  90. }else{//普通數據 
  91. $result[$key] = trim($value); 
  92. //刪除字符串兩端空白及其它預定義字符 
  93. //轉化數字 
  94. private function to_num($num){ 
  95. if(is_numeric($num)){ 
  96. return intval($num);//將變量轉為整數 
  97. }else
  98. return false; 
  99. //過換過濾字符串 
  100. private function filter_string($data){ 
  101. if($data===null){ 
  102. return false; 
  103. if(is_array($data)){ 
  104. foreach($data as $k=>$v){ 
  105. $data[$k] = htmlspecialchars($v,ENT_QUOTES); 
  106. //把一些預定義字符轉化為html實體 
  107. return $data
  108. }else{//普通字符串 
  109. return htmlspecialchars($data,ENT_QUOTES); 
  110. ?> 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凉城县| 浏阳市| 喜德县| 兰坪| 邹平县| 昔阳县| 临清市| 从化市| 福州市| 上栗县| 台州市| 西乌珠穆沁旗| 五莲县| 平陆县| 会宁县| 新平| 卢湾区| 伊宁市| 天峨县| 平乡县| 周宁县| 尼木县| 南开区| 安新县| 静海县| 房产| 宁海县| 延津县| 广元市| 竹山县| 平阳县| 南投县| 昂仁县| 浦城县| 龙胜| 即墨市| 日喀则市| 铜陵市| 大埔县| 南投市| 阳新县|