CakePHP:如何在评论帖子时停止插入/添加html标签?

shi*_*bly 0 javascript php jquery cakephp cakephp-1.3

这是评论的形式:

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');
Run Code Online (Sandbox Code Playgroud)

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.'
    )
);
Run Code Online (Sandbox Code Playgroud)

}

但在评论期间,有人可以输入评论表单的任何文本框,例如this =>

<script>
    alert("Hello world");
</script>
Run Code Online (Sandbox Code Playgroud)

然后在页面加载期间将显示此警报.我怎么能停止在数据库中插入这个html标签?我怎么检查这个html块?

dec*_*eze 6

有两种方法可以解决这个问题:清理转义字符串.清理意味着您可以删除所有不需要的内容.转义意味着您"禁用" 字符串中的任何特殊字符.输出时应始终转义用户提供的内容:

echo htmlspecialchars($comment['body']);
Run Code Online (Sandbox Code Playgroud)

可以选择清理字符串,但这可能很棘手.看看Cake的Sanitize课程.伟大的逃避现实也是合适的.