HTTP request class for easy POST/GET manipulation


/ Published in: PHP
Save to your folder(s)

Feel free to use this class however you'd like


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. class HttpReq {
  4.  
  5. public $param; // the returned POST/GET values
  6. public $cookie; // the returned COOKIE values
  7. public $session; // the returned SESSION values
  8. private $strength; // the strength of sanitization
  9.  
  10. /**
  11. * Class constructor takes one argument to set the strength of sanitization
  12. * @param string $strength values can be 'normal', 'strong', or 'strict'
  13. */
  14. public function __construct($strength='normal'){
  15. $this->param = array();
  16. $this->cookie = array();
  17. $this->session = array();
  18. $this->strength = $strength;
  19. }
  20.  
  21.  
  22. /**
  23. * Method to set, clean &/or sanitize a $_GET value if set
  24. * @param string $name the name of the value sought
  25. * @param boolean $urlDecode set to TRUE if the method should urldecode the value
  26. * @param boolean $san set to TRUE if the method should sanitize the value against XSS vulnerabilities
  27. * @return array
  28. */
  29. public function by_get($name='', $urlDecode=FALSE, $san=FALSE) {
  30.  
  31. if (isset($_GET[$name]))
  32. {
  33. if ($urlDecode && $san) {
  34. $this->param[$name] = $this->clean_data(
  35. $this->san_data($_GET[$name]),
  36. TRUE);
  37. }
  38. elseif ($urlDecode) {
  39. $this->param[$name] = $this->clean_data($_GET[$name], TRUE);
  40. }
  41. elseif ($san) {
  42. $this->param[$name] = $this->clean_data(
  43. $this->san_data($_GET[$name]),
  44. FALSE);
  45. }
  46. else {
  47. $this->param[$name] = $this->clean_data($_GET[$name], FALSE);
  48. }
  49. }
  50. else {
  51. $this->param[$name] = NULL;
  52. }
  53. return $this->param;
  54. }
  55.  
  56.  
  57. /**
  58. * Method to set, clean &/or sanitize a $_POST value if set
  59. * @param string $name the name of the value sought
  60. * @param boolean $urlDecode set to TRUE if the method should urldecode the value
  61. * @param boolean $san set to TRUE if the method should sanitize the value against XSS vulnerabilities
  62. * @return array
  63. */
  64. public function by_post($name='', $urlDecode=FALSE, $san=FALSE) {
  65.  
  66. if (isset($_POST[$name]))
  67. {
  68. if ($urlDecode && $san) {
  69. $this->param[$name] = $this->clean_data(
  70. $this->san_data($_POST[$name]),
  71. TRUE);
  72. }
  73. elseif ($urlDecode) {
  74. $this->param[$name] = $this->clean_data($_POST[$name], TRUE);
  75. }
  76. elseif ($san) {
  77. $this->param[$name] = $this->clean_data(
  78. $this->san_data($_POST[$name]),
  79. FALSE);
  80. }
  81. else {
  82. $this->param[$name] = $this->clean_data($_POST[$name], FALSE);
  83. }
  84. }
  85. else {
  86. $this->param[$name] = NULL;
  87. }
  88. return $this->param;
  89. }
  90.  
  91.  
  92. /**
  93. * Additional method to set a $_COOKIE value if set
  94. * @param string $name the name of the value sought
  95. */
  96. public function by_cookie($name='') {
  97. $this->cookie[$name] = (isset($_COOKIE[$name])) ?
  98. $_COOKIE[$name] : NULL;
  99.  
  100. return $this->cookie;
  101. }
  102.  
  103.  
  104. /**
  105. * Additional method to set a $_SESSION value if set
  106. * @param string $name the name of the value sought
  107. */
  108. public function by_session($name='') {
  109. $this->session[$name] = (isset($_SESSION[$name])) ?
  110. $_SESSION[$name] : NULL;
  111.  
  112. return $this->session;
  113. }
  114.  
  115.  
  116. /**
  117. * Private method to clean data
  118. * @param mixed $data
  119. * @param Boolean $isUrlEncoded
  120. */
  121. private function clean_data($data, $isUrlEncoded=FALSE) {
  122. return ($isUrlEncoded) ?
  123. strip_tags(trim($data));
  124. }
  125.  
  126.  
  127. /**
  128. * Private method to sanitize data
  129. * @param mixed $data
  130. */
  131. private function san_data($data) {
  132. switch($this->strength){
  133. default:
  134. return htmlspecialchars($data, ENT_QUOTES, "UTF-8");
  135. break;
  136. case 'strong':
  137. return htmlentities($data, ENT_QUOTES | ENT_IGNORE, "UTF-8");
  138. break;
  139. case 'strict':
  140. return urlencode($data);
  141. break;
  142. }
  143. }
  144.  
  145. }

URL: http://www.six-degrees.com/six-degrees.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.