How to prevent XSS attacks

Source: Internet
Author: User
How can we prevent XSS attacks? my comment function cannot prevent '> script alert (document. cookie) script
= '> Script alert (document. cookie) script
This type of code attacks, but it doesn't matter if I look at the CSDN Forum. what should I do to prevent such character attacks like the forum?

If there is no way, how can I write code if I only accept Chinese characters (in upper and lower cases), numbers, punctuation marks, and other characters used to write common articles?

Thank you!


Reply to discussion (solution)

Use the htmlspecialchars function to filter comments.

You can use PDO ~~

Forgive me for being dull. If I have the following code structure:
$ Bid = $ _ POST ['bid']
...
$ SQL = insert into "abc" ('bid') values ($ bid )....



How can I use filter? For example, how do I use the input name = bid and htmlspecialchars functions I submitted?
In addition, I learned how to use this function? Http://www.w3school.com.cn/php/func_string_htmlspecialchars.asp.pdf converts a predefined entity to an HTML entity. I don't think the characters have changed? What does this mean?
& (And number) become &
"(Double quotation marks)"
'(Single quotes)'
<(Less than) becomes <
> (Greater than) become>

Also, if the conversion happens, my understanding is that the conversion is displayed in the database. why is the style and content displayed on the page the original user input? Do you still need to parse a code or something?

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.