http://blog.qita.in/?post=494
- /**
- * 取得輸入統一入口
- */
- class cls_request
- {
- private $getdata; //儲存get的資料
- private $postdata; /ate $postdata; /儲存post的資料
- private $requestdata; //儲存request的資料
- private $filedata; //儲存file資料
- private $cookiedata; //儲存cookie
- static $_instance; //本類別的實例
-
- private function __construct()
- {
- $this->getdata = self::format_data($_GET);
- $this->postdata = self::format_data ($_POST);
- $this->requestdata = array_merge($this->getdata, $this->postdata);
- $this->cookiedata = self::format_data($_COOKIE);
- $this->filedata = self::format_data($_FILES);
- }
- /**
- * 類別的初始化方法
- *
- * @return cls_request 物件
- */
- public static function get_instance()
- {
- if(!(! self::$_instance instanceof self))
- {
- self::$_instance = new self();
- }
- return self::$_instance;
- }
-
- /**
- * 取得GET傳遞過來的數值變數
- *
- * @param string $key
- * @return int or big int
- */
- public function get_num($key)
- {
- if(!isset($this->getdata[$key]))
- {
- return false;
- }
- return $this->to_num($this->getdata[$key]);
- }
-
- /**
- * 取得POST傳遞過來的數值變數
- *
- * @param string $key
- * @return int or big int
- */
- public function post_num($key)
- {
- if(!isset($this->postdata[$key]))
- {
- return false;
- }
- return $this-> to_num($this->postdata[$key]);
- }
-
- /**
- * 取得REQUEST傳遞過來的數值變數
- *
- * @param string $key
- * @return int or big int
- */
- public function request_num($key)
- {
- if( !isset($this->requestdata[$key]))
- {
- return false;
- }
- return $this->to_num($this->requestdata[$key]);
- }
-
- /**
- * 取得Cookie傳遞過來的數值變數
- *
- * @param string $key
- * @return int or big int
- */
- public function cookie_num($key)
- {
- if(!isset($this->cookiedata[$key])))
- {
- return false;
- }
- return $this->to_num($this->cookiedata[$key]);
- }
-
- /**
- * 取得Files傳遞過來的變數值
- *
- * @param string $key
- * @return array
- */
- public function file_data($key)
- {
- return $this->filedata[$key];
- }
-
- /**
- * 取得GET傳遞過來的字串變數
- *
- * @param string $key
- * @param boolen $isfilter 是否過濾
- * @return string
- */
- public function get_string($key,$isfilter=true)
- {
- if(!isset($this->getdata[$key]))
- {
- return false;
- }
- if($isfilter)
- {
- return $this->filter_string($this->getdata[$key]);
- }
- else
- {
- return $this->getdata[$key];
- }
- }
-
- /**
- * 取得POST傳遞過來的字串變數
- *
- * @param string $key
- * @param boolen $isfilter 是否過濾
- * @return string
- */
- public function post_string($key,$isfilter=true)
- {
- if(!isset($this->postdata[$key]))
- {
- return false;
- }
- if($isfilter)
- {
- return $this->filter_string($this->postdata[$key]);
- }
- else
- {
- return $this->postdata[$key];
- }
- }
-
- /**
- * 取得REQUEST傳遞過來的字串變數
- *
- * @param string $key
- * @param boolen $isfilter 是否過濾
- * @return string
- */
- public function request_string($key,$isfilter=true)
- {
- if(!isset($this->requestdata[$key ]))
- {
- return false;
- }
- if($isfilter)
- {
- return $this->filter_string($this->requestdata[$key]);
- }
- else
- {
- return $this->requestdata[$key];
- }
- }
-
- /**
- * 取得COOKIE傳遞過來的字串變數
- *
- * @param string $key
- * @param boolen $isfilter 是否過濾
- * @return string
- */
- public function cookie_string($key,$isfilter=true)
- {
- if(!isset($this->cookiedata[$key]))
- {
- return false;
- }
- if($isfilter)
- {
- return $this->filter_string($this->cookiedata[$key]);
- }
- else
- {
- return $ this->cookiedata[$key];
- }
- }
-
- /**
- * 格式化資料,將資料轉存
- *
- * @param array $data
- */
- 私有函數format_data($data)
- {
- $result = array();
- if(!is_array($data))
- {
- $data = array();
- }
- while(list($key, $value) = every($data))
- {
- //處理checkbox之類別的資料
- if(is_array($value))
- {
- $result[$key]=$value;
- }
- else // 普通資料
- {
- $result[$key] = trim($value);
- }
- }
- return $result;
- }
- /**
- * 轉換為數字
- *
- * @param string $num
- * @return int or big int or false
- */
- 私有函數函數( $num)
- {
- if(is_numeric($num))
- {
- return intval($num);
- }
- else
- {
- return false;
- }
- }
- /**
- * 轉換過濾字串
- *
- * @param string/array $data
- * @return string
- */
- 私有函數filter_string($data)
- {
- if($data===NULL)
- {
- return false;
- }
- if (is_array($data))
- {
- foreach ($data as $k=>$v)
- {
- $data[ $ k] = htmlspecialchars($v, ENT_QUOTES);
- }
- return $data;
- }
- else
- {
- return htmlspecialchars($data, ENT
- {
- return htmlspecialchars($data, ENT_QUOTES);
-
} }
複製程式碼 |