- /**
- * 获取输入统一入口
- */
- class cls_request
- {
- private $getdata; //存储get的数据
- private $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
- */
- private function format_data($data)
- {
- $result = array();
- if(!is_array($data))
- {
- $data = array();
- }
- while(list($key, $value) = each($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
- */
- private function to_num($num)
- {
- if(is_numeric($num))
- {
- return intval($num);
- }
- else
- {
- return false;
- }
- }
- /**
- * 转换过滤字符串
- *
- * @param string/array $data
- * @return string
- */
- private function 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_QUOTES);
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/010820134921.html
来源: http://www.codesnippet.cn/detail/010820134921.html