What XSS attacks? PHP functions to prevent XSS attacks

Source: Internet
Author: User

XSS is called Cross Site Scripting. users intentionally or unintentionally enter malicious characters in the form, thus damaging the page performance!

Take a look at common malicious characters XSS input:

1. XSS input usually contains JavaScript scripts. For example, a malicious warning box is displayed: <script> alert ("XSS"); </script>

2. XSS input may also be an HTML code segment, for example:

(1). the webpage keeps refreshing <meta http-equiv = "refresh" content = "0;">

(2). Link embedding other websites <iframe src = http: // xxxx width = 250 height = 250> </iframe>


Test path to prevent XSS attacks:

 
In fact, there are many tools to test XSS attacks:
  • Paros proxy (http://www.parosproxy.org)
  • Fiddler (http://www.fiddlertool.com/fiddler)
  • Burp proxy (http://www.portswigger.net/proxy)
  • TamperIE (http://www.bayden.com/dl/TamperIESetup.exe)

How can PHP developers prevent XSS attacks?( Functions of php to prevent xss attacks)
You can use the following functions:
<? PHP/*** @ blog http://www.phpddt.com * @ param $ string * @ param $ low security level low */function clean_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 out this, you will know that xss attacks are amazing echo $ str;?>

Clean_xss () is used to filter malicious content!
In addition, this article draws on the IBM website this article: https://www.ibm.com/developerworks/cn/opensource/os-cn-php-xss/

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.