php统一入口,输入数据统一入口类

/**

* 获取输入统一入口

*/

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

*/

publicfunction 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

*/

publicfunction 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

*/

publicfunction 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

*/

publicfunction 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

*/

publicfunction 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

*/

privatefunction 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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值