简体   繁体   English

PHP数据库-不想显示JavaScript代码

[英]PHP databases - don't want to show javascript code

I have problem with PHP and JavaScript/CSS. 我在使用PHP和JavaScript / CSS时遇到问题。

I have database with table. 我有表数据库。 The table has a descriptions of articles. 该表对文章进行了描述。 I want to echo the descriptions of the articles from database. 我想从数据库中回显文章的描述。 Unfortunately many of them has a JavaScript or CSS included ( Then some article text), so when I use echo, it shows all of that code (and after that text). 不幸的是,其中许多都包含JavaScript或CSS(然后是一些文章文本),因此当我使用echo时,它将显示所有代码(在该文本之后)。 Is there any way to not show the JavaScript/CSS part and show only the text? 有什么办法不显示JavaScript / CSS部分而仅显示文本吗? For example with str_replace and regular expression? 例如使用str_replace和正则表达式? If yes, can somebody write me how it should look like? 如果是,有人可以写信给我它的外观吗?

Thanks for help and let me know if u need more info (code etc.) 感谢您的帮助,让我知道您是否需要更多信息(代码等)

Use HTMLPurifier - it will remove the scripts, css and any harmfull content from your articles. 使用HTMLPurifier-它会从您的文章中删除脚本,css和任何有害内容。 Since it is a CPU-intensive operations, it's better to run article trough HTMLPurifer before saving in the database, then to run it each time you are showing the article. 由于这是一项占用大量CPU的操作,因此最好在保存到数据库之前通过HTMLPurifer运行文章,然后在每次显示文章时都运行它。

If you're trying to remove tags from a user's post, you can call strip_tags . 如果您尝试从用户的帖子中删除标签,则可以调用strip_tags This will get rid of css links, script tags, etc. It will not get rid of the style attribute, but if you get rid of div , span , p , etc. that won't matter -- there will be no tag for it to reside on. 这将摆脱css链接,脚本标记等。它不会摆脱style属性,但是如果摆脱divspanp等,那将无关紧要-将没有标记它驻留。

As has been stated by others, it is generally best to sanitize your input (data from user before it goes into the DB), than it is to sanitize your output . 就像其他人所说的,通常最好是清理您的输入 (来自用户的数据,然后再进入DB),而不是清理输出

If you're trying to simply hide the JS and CSS from users, you can use Packer to obfusicate Javascript from less-savvy users, use Packer and use base 62 encoding. 如果您只是想向用户隐藏JS和CSS,则可以使用Packer对不太熟悉的用户隐藏Javascript,使用Packer并使用base 62编码。 The JS will still work but will look like jiberish. JS仍然可以使用,但是看起来像是刚硬的。 Be aware that more knowledgeable users can attempt to unobfusicate the code, so any critical security risks in the JS still exists. 请注意,知识渊博的用户可以尝试对代码进行模糊处理,因此JS中的任何重大安全风险仍然存在。 Don't think any JS that accesses your databases directly will be safe; 不要以为任何直接访问数据库的JS都是安全的。 instead remove database access from the Javascript for security. 而是从Java安全性中删除对数据库的访问。 If the JS is just to do fancy things like move elements around the page it's probably fine to just obfuscate it. 如果JS只是想做一些花哨的事情,例如在页面上移动元素,则最好对其进行混淆。

Only consider this if YOU have complete control and awareness of all JS included with the articles. 仅当您完全控制和理解本文中包含的所有JS时,才考虑这一点。 If this is something your anonmous or otherwise not 120% trusted users can upload, you need to kill that functionality and use HTML Purifier to remove any JS they might add. 如果这是您的匿名用户或不是120%受信任用户可以上传的内容,则您需要终止该功能并使用HTML Purifier删除他们可能添加的任何JS。 It is not safe to output user entered JS, for you or your users. 对于您或您的用户,输出用户输入的JS是不安全的。

For the CSS, I'm not sure why you want to hide it, and CSS can't be obfuscated quite like JS can; 对于CSS,我不确定为什么要隐藏它,并且CSS不能像JS一样被混淆。 the styles will still be in plain English, best you can do is butcher the class/id names and whitespace; 样式仍将使用简单的英语,最好的办法是屠宰类/ ID名称和空格; outputting CSS that YOU generated isn't a real security risk though, and even if people reverse engineer it I wouldn't be that afraid. 输出您生成的CSS并不是真正的安全风险,即使人们进行反向工程,我也不会那么害怕。

Again, if this is something anonymous/non trusted users can ADD to your site on their own, you don't want this at all, so remove the ability to upload CSS with an article using the HTML Purifier Darhazer mentioned. 同样,如果这是匿名/不受信任的用户可以自己添加到您的网站上的东西,那么您根本就不需要这样做,因此请删除使用提到的HTML Purifier Darhazer在文章中上传CSS的功能。

You can try the following regex to remove the script and css: 您可以尝试使用以下正则表达式删除脚本和CSS:

"<script[\d\D]*?>[\d\D]*?</script>"
"<style[\d\D]*?>[\d\D]*?</style>"

It should help, but it cannot remove all the scripts. 它应该有帮助,但是不能删除所有脚本。 Like onclick="javascript:alert(1)". 就像onclick =“ javascript:alert(1)”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM