我正在创建一个目录,用户可以在其中发布文章。为了获取要发布的值,我使用输入 html 元素并使用 $.post
将数据保存在数据库中。
但是,我有一个问题,如果用户在文章中编写一些 html 代码,它会保存格式化代码。
例如,如果输入的值是:
<input type="text" value="this is an article title <script>$("body").remove();</script>">
当帖子提交时,页面也会加载 js 脚本,删除
body
。如何避免这种情况并告诉输入字段中有一个脚本,或者将脚本格式化为显示为文本?
最佳答案
<?php
//Simple answer
#when you echo data results from the database. consider code below..
#Assuming you are at the last process of echoling the data out.
$data='THIS WOULD BE YOUR DATA OBJECT OR VARIABLE CONTAINING DATA FROM THE DATABASE';
#then...
$data=htmlentities($data);
#or
$data=strip_tags($data);
#or
$data=htmlspecialchars($data);
#just be assured that tags or code will not be executed by the browser once above is included!
echo $data;
关于javascript - 防止输入提交 html 代码/脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39933661/