您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
4-4 xss攻击之过滤防范(script)
发布时间:2023-04-30 17:53:15编辑:雪饮阅读()
-
对于像是这样的一个请求
http://www.xyyii.com/basic/web/index.php?r=hello/index&script=<script>alert(111);</script>
如果说将这个script变量接收了并输出到页面上,则页面会有弹窗。
要解决这个问题则可以使用\yii\helpers\Html::encode方法将script传入进行处理,处理结束后再输出就没有这个问题了。
下面的实例就是有使用到\yii\helpers\Html::encode函数,和没有使用到\yii\helpers\Html::encode函数的输出对比。
<?php
namespace app\controllers;
use yii\web\Controller;
class HelloController extends Controller{
public function actionIndex(){
$script=\YII::$app->request->get('script');
echo $script;
}
public function actionIndex2(){
$script=\YII::$app->request->get('script');
echo \yii\helpers\Html::encode($script);
}
}
关键字词:script