HTTP请求类,便于POST/GET操作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTTP请求类,便于POST/GET操作相关的知识,希望对你有一定的参考价值。
Feel free to use this class however you'd like
<?php class HttpReq { public $param; // the returned POST/GET values public $cookie; // the returned COOKIE values public $session; // the returned SESSION values private $strength; // the strength of sanitization /** * Class constructor takes one argument to set the strength of sanitization * @param string $strength values can be 'normal', 'strong', or 'strict' */ public function __construct($strength='normal'){ $this->strength = $strength; } /** * Method to set, clean &/or sanitize a $_GET value if set * @param string $name the name of the value sought * @param boolean $urlDecode set to TRUE if the method should urldecode the value * @param boolean $san set to TRUE if the method should sanitize the value against XSS vulnerabilities * @return array */ public function by_get($name='', $urlDecode=FALSE, $san=FALSE) { { if ($urlDecode && $san) { $this->param[$name] = $this->clean_data( $this->san_data($_GET[$name]), TRUE); } elseif ($urlDecode) { $this->param[$name] = $this->clean_data($_GET[$name], TRUE); } elseif ($san) { $this->param[$name] = $this->clean_data( $this->san_data($_GET[$name]), FALSE); } else { $this->param[$name] = $this->clean_data($_GET[$name], FALSE); } } else { $this->param[$name] = NULL; } return $this->param; } /** * Method to set, clean &/or sanitize a $_POST value if set * @param string $name the name of the value sought * @param boolean $urlDecode set to TRUE if the method should urldecode the value * @param boolean $san set to TRUE if the method should sanitize the value against XSS vulnerabilities * @return array */ public function by_post($name='', $urlDecode=FALSE, $san=FALSE) { { if ($urlDecode && $san) { $this->param[$name] = $this->clean_data( $this->san_data($_POST[$name]), TRUE); } elseif ($urlDecode) { $this->param[$name] = $this->clean_data($_POST[$name], TRUE); } elseif ($san) { $this->param[$name] = $this->clean_data( $this->san_data($_POST[$name]), FALSE); } else { $this->param[$name] = $this->clean_data($_POST[$name], FALSE); } } else { $this->param[$name] = NULL; } return $this->param; } /** * Additional method to set a $_COOKIE value if set * @param string $name the name of the value sought */ public function by_cookie($name='') { $_COOKIE[$name] : NULL; return $this->cookie; } /** * Additional method to set a $_SESSION value if set * @param string $name the name of the value sought */ public function by_session($name='') { $_SESSION[$name] : NULL; return $this->session; } /** * Private method to clean data * @param mixed $data * @param Boolean $isUrlEncoded */ private function clean_data($data, $isUrlEncoded=FALSE) { return ($isUrlEncoded) ? } /** * Private method to sanitize data * @param mixed $data */ private function san_data($data) { switch($this->strength){ default: break; case 'strong': break; case 'strict': break; } } }
以上是关于HTTP请求类,便于POST/GET操作的主要内容,如果未能解决你的问题,请参考以下文章
HTTP Basic Auth 的 POST / GET / PUT / DELETE 请求 By Java
HTTP Basic Auth 的 POST / GET / PUT / DELETE 请求 By Java