php 基础类库-获取输入数据统一入口
由 admin 发表于 17:25<?php /** * 获取输入数据统一入口 */ class cls_request { protected $get_data; //存储get方式获取的数据 protected $post_data; //存储post方式获取的数据 protected $request_data; //存储get,post,cookie方式获取的数据 protected $files_data; //存储file方式获取的数据 protected $cookie_data; //存储cookie数据 static public $_instance;//本类的实例 /** * 构造函数 */ protected function __construct() { $this->get_data = self::format_data($_GET); $this->post_data = self::format_data($_POST); $this->request_data = array_merge($this->get_data, $this->post_data); $this->cookie_data = self::format_data($_COOKIE); $this->files_data = self::format_data($_FILES); } /** * 类的初始化方法 * * @return cls_request 对象 */ static public 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->get_data[$key])) { return false; } return $this->to_num($this->get_data[$key]); } /** * 获取GET传递过来的数值变量 * 满足静态方式调用 * * @param string $key * @return int or big int */ static function get_num_static($key) { $instance = self::get_instance(); return $instance->get_num($key); } /** * 获取GET传递过来的字符串变量 * * @param string $key 参数名称 * @param boolen $is_filter 是否过滤 * @return string */ public function get_string($key, $is_filter=true) { if (!isset($this->get_data[$key])) { return false; } if ($is_filter) { return $this->filter_string($this->get_data[$key]); } else { return $this->get_data[$key]; } } /** * 获取GET传递过来的字符串变量 * 满足静态方式调用 * * @param string $key * @param boolen $is_filter 是否过滤 * @return string */ public function get_string_static($key, $is_filter=true) { $instance = self::get_instance(); return $instance->get_string($key, $is_filter); } /** * 获取POST传递过来的数值变量 * * @param string $key * @return int or big int */ public function post_num($key) { if (!isset($this->post_data[$key])) { return false; } return $this->to_num($this->post_data[$key]); } /** * 获取POST传递过来的数值变量 * 满足静态方式调用 * * @param string $key * @return int or big int */ static function post_num_static($key) { $instance = self::get_instance(); return $instance->post_num($key); } /** * 获取POST传递过来的字符串变量 * * @param string $key 参数名称 * @param boolen $is_filter 是否过滤 * @return string */ public function post_string($key, $is_filter=true) { if (!isset($this->post_data[$key])) { return false; } if ($is_filter) { return $this->filter_string($this->post_data[$key]); } else { return $this->post_data[$key]; } } /** * 获取POST传递过来的字符串变量 * 满足静态方式调用 * * @param string $key * @param boolen $isfilter 是否过滤 * @return string */ public function post_string_static($key, $is_filter=true) { $instance = self::get_instance(); return $instance->post_string($key, $is_filter); } /** * 获取REQUEST传递过来的数值变量 * * @param string $key * @return int or big int */ public function request_num($key) { if (!isset($this->request_data[$key])) { return false; } return $this->to_num($this->request_data[$key]); } /** * 获取REQUEST传递过来的数值变量 * 满足静态方式调用 * * @param string $key * @return int or big int */ static function request_num_static($key) { $instance = self::get_instance(); return $instance->request_num($key); } /** * 获取REQUEST传递过来的字符串变量 * * @param string $key 参数名称 * @param boolen $is_filter 是否过滤 * @return string */ public function request_string($key, $is_filter=true) { if (!isset($this->request_data[$key])) { return false; } if ($is_filter) { return $this->filter_string($this->request_data[$key]); } else { return $this->request_data[$key]; } } /** * 获取REQUEST传递过来的字符串变量 * 满足静态方式调用 * * @param string $key 参数名称 * @param boolen $is_filter 是否过滤 * @return string */ public function request_string_static($key, $is_filter=true) { $instance = self::get_instance(); return $instance->request_string($key, $is_filter); } /** * 获取COOKIE传递过来的数值变量 * * @param string $key * @return int or big int */ public function cookie_num($key) { if (!isset($this->cookie_data[$key])) { return false; } return $this->to_num($this->cookie_data[$key]); } /** * 获取COOKIE传递过来的数值变量 * 满足静态方式调用 * * @param string $key * @return int or big int */ static function cookie_num_static($key) { $instance = self::get_instance(); return $instance->cookie_num($key); } /** * 获取COOKIE传递过来的字符串变量 * * @param string $key 参数名称 * @param boolen $is_filter 是否过滤 * @return string */ public function cookie_string($key, $is_filter=true) { if (!isset($this->cookie_data[$key])) { return false; } if ($is_filter) { return $this->filter_string($this->cookie_data[$key]); } else { return $this->cookie_data[$key]; } } /** * 获取COOKIE传递过来的字符串变量 * 满足静态方式调用 * * @param string $key * @param boolen $isfilter 是否过滤 * @return string */ public function cookie_string_static($key, $is_filter=true) { $instance = self::get_instance(); return $instance->cookie_string($key, $is_filter); } /** * 获取Files传递过来的变量值 * * @param string $key * @return array */ public function file_data($key) { return $this->files_data[$key]; } /** * 获取Files传递过来的变量值 * * @param string $key * @return array */ public function file_data_static($key) { $instance = self::get_instance(); return $instance->file_data($key); } /** * 格式化数据,将数据转存 * * @param array $data */ protected function format_data($data) { $result = array(); foreach ($data as $key=>$value) { //处理checkbox类型的数据 if (is_array($value)) { foreach ($value as $k=>$v) { $value[$k] = trim($v); } $result[$key] = $value; } else { $result[$key] = trim($value); } } return $result; } /** * 转换为数字 * * @param string $num * @return int or big int or false */ protected function to_num($data) { if (is_numeric($data)) { return intval($data); } elseif (is_array($data)) //checkbox类型数据 { foreach ($data as $key=>$value) { $data[$key] = intval($value); } return $data; } else { return false; } } /** * 转换过滤字符串 * * @param string/array $data * @return string */ protected function filter_string($data) { if ($data === null) { return false; } //checkbox类型数据 if (is_array($data)) { foreach ($data as $key=>$value) { $data[$key] = htmlspecialchars($value); } return $data; } else { return htmlspecialchars($data); } } } ?>