开发者

What is a function that will allow output with HTML and avoid XSS attacks

开发者 https://www.devze.com 2023-03-30 21:46 出处:网络
I am looking for a way or function that will allow me to display data from my mySQL database. The users are allowed t开发者_开发百科o post articles, that I use mysql_real_escape_string to avoid SQL in

I am looking for a way or function that will allow me to display data from my mySQL database. The users are allowed t开发者_开发百科o post articles, that I use mysql_real_escape_string to avoid SQL injections before inserting their post in the DB.

For my testing pursposes I write in a text area my post with tags like <b> <a> <i> <li>. Later I will use an editor like this one here on Stackoverflow to help users with their posts.

However, I am aware of XSS and just echoing straight from the DB may lead to XSS attacks. So, I choosed for my tests to output the content with htmlentities or htmlspecialchars. None of them will show me the post correctly with html.

Therefore, I used strip tags but as far as I know and read, is not safe.

What is a function that you may use too, that will let me output the data correctly, just like this and prevent XSS?


If you want to display html correctly you should print plain html as you get it. But for avoiding XSS try to remove javascript tags and don't allow load images from external resources.

0

精彩评论

暂无评论...
验证码 换一张
取 消