这是注释表单:
echo $this->Form->create('Comment',array('url'=>array('controller' => 'comments', 'action' =>'add', $listposts['Post']['id']) ) );
echo $this->Form->input('post_id',array('type'=>'hidden','style'=>'width:30%','value'=>$listposts['Post']['id']));
echo $this->Form->input('name',array('style'=>'width:30%'));
echo $this->Form->input('email',array('style'=>'width:30%'));
echo $this->Form->input('body',array('rows'=>'5'));
echo $this->Form->end('Comment');
注释。php模型=>
var $useTable='comments';
var $belongsTo = array('Post');
var $validate = array(
'name' => array(
'required' => true,
'rule' => 'notEmpty',
'allowEmpty' => false,
'message' => 'Enter Name.'
),
'email' => array(
'required' => true,
'rule' => 'notEmpty',
'allowEmpty' => false,
'message' => 'Enter Email.'
),
'body' => array(
'required' => true,
'rule' => 'notEmpty',
'allowEmpty' => false,
'message' => 'Enter Body.'
)
);
}
但是在评论过程中,人们可以在评论表单的任何文本框中输入这样的内容=>
<script>
alert("Hello world");
</script>
那么这个警告将在页面加载期间显示。我怎么能停止在数据库中插入这个html标签?如何检查这个html块
有两种处理方法:对进行消毒或转义字符串。"消毒"意味着删除所有不需要的内容。转义意味着"禁用"字符串中的任何特殊字符。当输出用户提供的内容时,应该总是转义:
echo htmlspecialchars($comment['body']);
可以选择想要对字符串进行消毒,但这可能比较棘手。看看Cake的Sanitize
课程。
您可以使用:strip_tags()
或htmlspecialchars()
$str = "<script>alert('Hello world');</script>";
echo "strip_tags = " . strip_tags($str);
echo "htmlspecialchars = " . htmlspecialchars($str);
演示使用
http://php.net/manual/en/function.htmlspecialchars.php