首頁 後端開發 php教程 輸入資料統一入口類

輸入資料統一入口類

Jul 25, 2016 am 09:01 AM

輸入資料統一入口類 http://blog.qita.in/?post=494
  1. /**
  2. * 取得輸入統一入口
  3. */
  4. class cls_request
  5. {
  6. private $getdata; //儲存get的資料
  7. private $postdata; /ate $postdata; /儲存post的資料
  8. private $requestdata; //儲存request的資料
  9. private $filedata; //儲存file資料
  10. private $cookiedata; //儲存cookie
  11. static $_instance; //本類別的實例
  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. * 類別的初始化方法
  22. *
  23. * @return cls_request 物件
  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傳遞過來的數值變數
  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. * 取得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. * 取得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. * 取得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. * 取得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傳遞過來的字串變數
  101. *
  102. * @param string $key
  103. * @param boolen $isfilter 是否過濾
  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. * 取得POST傳遞過來的字串變數
  123. *
  124. * @param string $key
  125. * @param boolen $isfilter 是否過濾
  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. * 取得REQUEST傳遞過來的字串變數
  145. *
  146. * @param string $key
  147. * @param boolen $isfilter 是否過濾
  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. * 取得COOKIE傳遞過來的字串變數
  167. *
  168. * @param string $key
  169. * @param boolen $isfilter 是否過濾
  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. * 格式化資料,將資料轉存
  189. *
  190. * @param array $data
  191. */
  192. 私有函數format_data($data)
  193. {
  194. $result = array();
  195. if(!is_array($data))
  196. {
  197. $data = array();
  198. }
  199. while(list($key, $value) = every($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. * 轉換為數字
  215. *
  216. * @param string $num
  217. * @return int or big int or false
  218. */
  219. 私有函數函數( $num)
  220. {
  221. if(is_numeric($num))
  222. {
  223. return intval($num);
  224. }
  225. else
  226. {
  227. return false;
  228. }
  229. }
  230. /**
  231. * 轉換過濾字串
  232. *
  233. * @param string/array $data
  234. * @return string
  235. */
  236. 私有函數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
  253. {
  254. return htmlspecialchars($data, ENT_QUOTES);
}
}
複製程式碼


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

php中的捲曲:如何在REST API中使用PHP捲曲擴展 php中的捲曲:如何在REST API中使用PHP捲曲擴展 Mar 14, 2025 am 11:42 AM

PHP客戶端URL(curl)擴展是開發人員的強大工具,可以與遠程服務器和REST API無縫交互。通過利用Libcurl(備受尊敬的多協議文件傳輸庫),PHP curl促進了有效的執行

在Codecanyon上的12個最佳PHP聊天腳本 在Codecanyon上的12個最佳PHP聊天腳本 Mar 13, 2025 pm 12:08 PM

您是否想為客戶最緊迫的問題提供實時的即時解決方案? 實時聊天使您可以與客戶進行實時對話,並立即解決他們的問題。它允許您為您的自定義提供更快的服務

解釋PHP中晚期靜態結合的概念。 解釋PHP中晚期靜態結合的概念。 Mar 21, 2025 pm 01:33 PM

文章討論了PHP 5.3中介紹的PHP中的晚期靜態結合(LSB),允許靜態方法的運行時間分辨率調用以更靈活的繼承。 LSB的實用應用和潛在的觸摸

在PHP API中說明JSON Web令牌(JWT)及其用例。 在PHP API中說明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

框架安全功能:防止漏洞。 框架安全功能:防止漏洞。 Mar 28, 2025 pm 05:11 PM

文章討論了框架中的基本安全功能,以防止漏洞,包括輸入驗證,身份驗證和常規更新。

自定義/擴展框架:如何添加自定義功能。 自定義/擴展框架:如何添加自定義功能。 Mar 28, 2025 pm 05:12 PM

本文討論了將自定義功能添加到框架上,專注於理解體系結構,識別擴展點以及集成和調試的最佳實踐。

如何用PHP的cURL庫發送包含JSON數據的POST請求? 如何用PHP的cURL庫發送包含JSON數據的POST請求? Apr 01, 2025 pm 03:12 PM

使用PHP的cURL庫發送JSON數據在PHP開發中,經常需要與外部API進行交互,其中一種常見的方式是使用cURL庫發送POST�...

See all articles