本篇文章介紹了設置tp防止xss攻擊的方法,希望對學習thinkphp的朋友有幫助!
Thinkphp防止XSS攻擊的方法
1 如果您的項目沒有富文本編輯器 然后就可以使用全局過濾方法 在application下面的config配置文件 加上 htmlspecialchars
//?默認全局過濾方法?用逗號分隔多個 'default_filter'?=>?'htmlspecialchars',
如果有富文本編輯器的話 就不適合 使用這種防XSS攻擊
立即學習“PHP免費學習筆記(深入)”;
(推薦教程:thinkphp教程)
那么使用 composer 安裝插件來處理
命令
composer?require?ezyang/htmlpurifier
安裝成功以后在application 下面的 common.php 放公共函數的地方添加如下代碼
if?(!function_exists('remove_xss'))?{ ????//使用htmlpurifier防范xss攻擊 ????function?remove_xss($string){ ????//composer安裝的,不需要此步驟。相對index.php入口文件,引入HTMLPurifier.auto.php核心文件 ????//?require_once?'./plugins/htmlpurifier/HTMLPurifier.auto.php'; ????//?生成配置對象 ????$cfg?=?HTMLPurifier_Config::createDefault(); ????//?以下就是配置: ????$cfg?->?set('Core.Encoding',?'UTF-8'); ????//?設置允許使用的HTML標簽 ????$cfg?->?set('HTML.Allowed','div,b,strong,i,em,a[href|title],ul,ol,li,br,p[style],span[style],img[width|height|alt|src]'); ????//?設置允許出現的CSS樣式屬性 ????$cfg?->?set('CSS.AllowedProperties',?'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align'); ????//?設置a標簽上是否允許使用target="_blank" ????$cfg?->?set('HTML.TargetBlank',?TRUE); ????//?使用配置生成過濾用的對象 ????$obj?=?new?HTMLPurifier($cfg); ????//?過濾字符串 ????return?$obj?->?purify($string); }
然后在 application目錄下的config.php 配置文件
把這個過濾方法改成那個方法名即可
結合框架的使用 和插件的使用可以使用這個 上面的代碼可以可以直接使用的
也可以針對某個字段進行xss驗證
1 修改 command的文件把改成這個 ‘default_filter’ => ‘htmlspecialchars’,
2 然后在你要更改的字段 上面 修改成
相關推薦:PHP視頻教程,學習地址:https://www.php.cn/course/list/29/type/2.html
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END