PHP protection against XSS attacks

Source: Internet
Author: User

XSS is all called Cross Site Scripting, and the user intentionally or unintentionally enters some malicious characters in the form, which destroys the performance of the page!

Look at common malicious characters for XSS input:

1.XSS input typically contains JavaScript scripts, such as pop-up malicious warning boxes: <script>alert ("XSS");</script>

The 2.XSS input may also be an HTML code snippet, such as:

(1). The webpage constantly refreshes <meta http-equiv= "refresh" content= "0;" >

(2). Links embedded in other websites <iframe src=http://xxxx width=250 height=250></iframe>

<?PHP/** * @blog http://www.phpddt.com * @param $string * @param $low safety level low*/    functionCLEAN_XSS (&$string,$low=False)    {        if(!Is_array($string ))        {            $string=Trim($string ); $string=Strip_tags($string ); $string=Htmlspecialchars($string ); if($low)            {                return True; }            $string=Str_replace(Array(‘"‘, "\\", "‘", "/", "..", ".. /", "./", "//" ), ‘‘,$string ); $no= '/%0[0-8bcef]/'; $string=Preg_replace($no, ‘‘,$string ); $no= '/%1[0-9a-f]/'; $string=Preg_replace($no, ‘‘,$string ); $no= '/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]+/s '; $string=Preg_replace($no, ‘‘,$string ); return True; }        $keys=Array_keys($string ); foreach($keys  as $key) {CLEAN_XSS ($string[$key] ); }    }    //just a test    $str= ' Phpddt.com<meta http-equiv= ' "Refresh" content= "0;" > '; CLEAN_XSS ($str);//If you comment this out, you know the XSS attack is awesome.    Echo $str; ?>

PHP protection against XSS attacks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.