secure input (xss, sql injection)


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



Copy this code and paste it in your HTML
  1. function secure_input($val, $strip = 1, $xss = 1, $charset = 'UTF-8')
  2. {
  3. if (is_array($val))
  4. {
  5. $output = array();
  6. foreach ($val as $key => $data)
  7. {
  8. $output[$key] = secure_input($data, $strip, $charset);
  9. }
  10. return $output;
  11. }
  12. else
  13. {
  14. if ($xss > 0)
  15. {
  16. // code by nicolaspar
  17. $val = preg_replace('/([\x00-\x08][\x0b-\x0c][\x0e-\x20])/', '', $val);
  18. $search = 'abcdefghijklmnopqrstuvwxyz';
  19. $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  20. $search .= '1234567890!@#$%^&*()';
  21. $search .= '~`";:?+/={}[]-_|\'\\';
  22.  
  23. for ($i = 0; $i < strlen($search); $i++)
  24. {
  25. $val = preg_replace('/(&#[x|X]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
  26. $val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
  27. }
  28.  
  29. $ra1 = Array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
  30. $ra2 = Array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
  31.  
  32. $ra = array_merge($ra1, $ra2);
  33. $found = true;
  34.  
  35. while ($found == true)
  36. {
  37. $val_before = $val;
  38. for ($i = 0; $i < sizeof($ra); $i++)
  39. {
  40. $pattern = '/';
  41. for ($j = 0; $j < strlen($ra[$i]); $j++)
  42. {
  43. if ($j > 0)
  44. {
  45. $pattern .= '(';
  46. $pattern .= '(&#[x|X]0{0,8}([9][a][b]);?)?';
  47. $pattern .= '|(&#0{0,8}([9][10][13]);?)?';
  48. $pattern .= ')?';
  49. }
  50. $pattern .= $ra[$i][$j];
  51. }
  52. $pattern .= '/i';
  53. $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2);
  54. $val = preg_replace($pattern, $replacement, $val);
  55. if ($val_before == $val)
  56. {
  57. $found = false;
  58. }
  59. }
  60. }
  61. }
  62.  
  63. // Strip HTML tags
  64. if ($strip > 0)
  65. {
  66. $val = strip_tags($val);
  67. }
  68.  
  69. // Encode special chars
  70. $val = htmlentities($val, ENT_QUOTES, $charset);
  71.  
  72. {
  73. }
  74. else
  75. {
  76. }
  77. }
  78. }
  79. echo secure_input($foo);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.