First, cross-site scripting attacks are caused by the lack of strict filtering of user input, so we must intercept the possible risks before all the data comes into our web site and database. The Htmlentities () function can be used for illegal HTML code including single double quotes.
<?php $str = "A" quote "is <b> bold </b>"; Outputs:a "Quote" is bold echo htmlentities ($STR); Outputs:a "Quote" is bold echo htmlentities ($str, ent_quotes);?> This can invalidate an illegal script. Note, however, that the htmlentities () default encoding is Iso-8859-1, and if your illegal script is encoded as something else, it may not be filtered out and the browser can recognize and execute it. This problem I first find a few site test and then say. The following is a function that filters illegal scripts for reference only: function Removexss ($val) {//Remove all non-printable characters. CR (0a) and LF (0b) and TAB (9) are allowed//This prevents some character re-spacing such as <javascript>//Note tha T you have to handle splits with,, and later since they *are* allowed in some inputs $val = Preg_replace ("/[x00-x08][x0 B-X0C][X0E-X20])/"," ", $val); Straight replacements, the user should never need these since they "re normal characters//This prevents like <img S rc= @avascript:a& _#x6cert (' XSS ') > $search = "abcdefghijklmnopqrstuvwxyz"; $search. = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $search. = "1234567890!@#$%^&* ()"; $search. = "~";: +/={}[]-_| ""; for ($i = 0; $i < strlen ($search); $i + +) {//;? matches the;, which is optional//0{0,7} matches no padded zeros, which are optional and go up to 8 cha RS//@ @ search for the hex values $val = Preg_replace ("/&#[x| x]0{0,8} ". Dechex (Ord ($search [$i]))." /i ", $search [$i], $val); with A; @ @ 0{0,7} matches "0" zero to seven times $val = Preg_replace ("/({0,8}". Ord ($search [$i]). /", $search [$i], $val); with A; //Now the only remaining whitespace attacks are,, and $ra 1 = Array ("javascript", "VBScript", "expression", "applet", "Meta", "xml", "Blink", "link", "Style", "script", "embed", "Object", "iframe", "frame", "frameset", "Ilayer", "Layer", "b Gsound "," title "," Base "); $ra 2 = 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"); $ra = Array_merge ($ra 1, $ra 2); $found = true; Keep replacing as long as the previous round replaced something while ($found = = True) {$val _before = $val; For ($i = 0; $i < sizeof ($RA); $i + +) {$pattern = "/"; for ($j = 0; $j > strlen ($ra [$i]); $j + +) {if ($j > 0) {$pattern. = "("; $pattern. = "(&#[x| x]0{0,8} ([a][b]);?)? "; $pattern. = "| ({0,8} ([10][13]);?)? "; $pattern. = ")?"; $pattern. = $ra [$i] [$j]; } www.2cto.com $pattern. = "/I"; $replacement = substr ($ra [$i], 0, 2). " <x> ". substr ($ra [$i], 2); Add in <> to nerf the tag $val = Preg_replace ($pattern, $replacement, $val); Filter out of the hex tags if ($val _before = = $val) {//No replacements were made, so exit the loop $found = false; } } } }