Many domestic forums have a cross-site scripting loophole, foreign also many such examples, even Google has appeared, but in early December revised. (Editor's note: For cross-site scripting exploits, readers can refer to the "detailed XSS cross-site scripting Attack"). Cross-station attacks are easy to construct, and very covert, not easy to be Chage (usually steal information immediately jump back to the original page).
How to attack, do not explain this (also do not ask me), mainly talk about how to prevent. First, cross-site scripting attacks are due to the lack of strict filtering of user input, so we have to intercept the possible dangers before all data enters our site and database. For illegal HTML code including single double quotes, you can use Htmlentities ().
$str = "A ' quote ' is
bold ";
Outputs:a ' Quote ' is <b>bold</b>
echo htmlentities ($STR);
Outputs:a & #039;quote& #039; Is <b>bold</b>
Echo htmlentities ($str, ent_quotes);
? >
This will invalidate the 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.
This provides a function to filter illegal scripts:
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
// Note this you have to handle splits with,, and later since they *are* allowed in some inputs
$val = Preg_replace ( '/([x00-x08][x0b-x0c][x0e-x20])/', ', $val);
//Straight replacements, the user should never need these since they ' re normal characters
//This prevents Li Ke _#x6c& #X65 & #X72 & #X74 & #X28 & #X27 & #X58 & #X53 & #X53 & #X27 &# x29>
$search = ' abcdefghijklmnopqrstuvwxyz ';
$search. = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ ';
$search. = ' 1234567890!@#$%^&* () ';
$search. = ' ~ ';:/={}[]-_| ';