Laravel5中防止XSS跨站攻擊的方法
本文實例講述了Laravel5中防止XSS跨站攻擊的方法。分享給大家供大家參考,具體如下:
Laravel 5本身沒有這個能力來防止xss跨站攻擊了,但是這它可以使用Purifier 擴(kuò)展包集成 HTMLPurifier 防止 XSS 跨站攻擊。
1、安裝
HTMLPurifier 是基于 PHP 編寫的富文本 HTML 過濾器,通常我們可以使用它來防止 XSS 跨站攻擊,更多關(guān)于 HTMLPurifier的詳情請參考其官網(wǎng):http://htmlpurifier.org/。Purifier 是在 Laravel 5 中集成 HTMLPurifier 的擴(kuò)展包,我們可以通過 Composer 來安裝這個擴(kuò)展包:
composer require mews/purifier
安裝完成后,在配置文件config/app.php的providers中注冊HTMLPurifier服務(wù)提供者:
'providers' => [ // ... Mews\Purifier\PurifierServiceProvider::class, ] 然后在aliases中注冊Purifier門面: 'aliases' => [ // ... 'Purifier' => Mews\Purifier\Facades\Purifier::class, ]
2、配置
要使用自定義的配置,發(fā)布配置文件到config目錄:
php artisan vendor:publish
這樣會在config目錄下生成一個purifier.php文件:
return [ 'encoding' => 'UTF-8', 'finalize' => true, 'preload' => false, 'cachePath' => null, 'settings' => [ 'default' => [ 'HTML.Doctype' => 'XHTML 1.0 Strict', 'HTML.Allowed' => 'div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]', 'CSS.AllowedProperties' => 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align', 'AutoFormat.AutoParagraph' => true, 'AutoFormat.RemoveEmpty' => true ], 'test' => [ 'Attr.EnableID' => true ], "youtube" => [ "HTML.SafeIframe" => 'true', "URI.SafeIframeRegexp" => "%^(http://|https://|//)(www.youtube.com/embed/|player.vimeo.com/video/)%", ], ], ];
3、使用示例
可以使用輔助函數(shù)clean:
clean(Input::get('inputname'));
或者使用Purifier門面提供的clean方法:
Purifier::clean(Input::get('inputname'));
還可以在應(yīng)用中進(jìn)行動態(tài)配置:
clean('This is my H1 title', 'titles');
clean('This is my H1 title', array('Attr.EnableID' => true));
或者你也可以使用Purifier門面提供的方法:
Purifier::clean('This is my H1 title', 'titles');
Purifier::clean('This is my H1 title', array('Attr.EnableID' => true));
php防止xss攻擊
<?PHP
function clean_xss(&$string, $low = False)
{
if (! is_array ( $string ))
{
$string = trim ( $string );
$string = strip_tags ( $string );
$string = htmlspecialchars ( $string );
if ($low)
{
return True;
}
$string = str_replace ( array ('"', "\\", "'", "/", "..", "../", "./", "http://" ), '', $string );
$no = '/%0[0-8bcef]/';
$string = preg_replace ( $no, '', $string );
$no = '/%1[0-9a-f]/';
$string = preg_replace ( $no, '', $string );
$no = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';
$string = preg_replace ( $no, '', $string );
return True;
}
$keys = array_keys ( $string );
foreach ( $keys as $key )
{
clean_xss ( $string [$key] );
}
}
//just a test
$str = 'jb51.net<meta http-equiv="refresh" content="0;">';
clean_xss($str); //如果你把這個注釋掉,你就知道xss攻擊的厲害了
echo $str;
?>
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設(shè)計有所幫助。
- 詳解Laravel5.6 Passport實現(xiàn)Api接口認(rèn)證
- 在Laravel5.6中使用Swoole的協(xié)程數(shù)據(jù)庫查詢
- 淺析Laravel5中隊列的配置及使用
- Laravel5框架自定義錯誤頁面配置操作示例
- Laravel5中Cookie的使用詳解
- Laravel5中contracts詳解
- Laravel5權(quán)限管理方法詳解
- laravel5.4+vue+element簡單搭建的示例代碼
- CKeditor4 字體顏色功能配置方法教程
- 手把手教你 CKEDITOR 4 擴(kuò)展插件制作
- CKEditor 4.4.1 添加代碼高亮顯示插件功能教程【使用官方推薦Code Snippet插件】
- Laravel5.6框架使用CKEditor5相關(guān)配置詳解
相關(guān)文章
使用Yii2實現(xiàn)主從數(shù)據(jù)庫設(shè)置
大家應(yīng)該都知道,當(dāng)項目做大了,數(shù)據(jù)庫主從還是不可少的。使用Yii框架開發(fā),如何設(shè)置數(shù)據(jù)庫的主從呢?其實很簡單。下面這篇文章就給大家詳細(xì)介紹了使用Yii2實現(xiàn)主從數(shù)據(jù)庫設(shè)置的方法,文中介紹的很詳細(xì),相信對大家的理解和學(xué)習(xí)很有幫助,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
PHP file_get_contents函數(shù)讀取遠(yuǎn)程數(shù)據(jù)超時的解決方法
這篇文章主要介紹了PHP file_get_contents函數(shù)讀取遠(yuǎn)程數(shù)據(jù)超時的解決方法,本文直接給出解決方法代碼,需要的朋友可以參考下2015-05-05
PHP新特性之字節(jié)碼緩存和內(nèi)置服務(wù)器
這篇文章主要介紹了PHP新特性之字節(jié)碼緩存和內(nèi)置服務(wù)器的相關(guān)資料,需要的朋友可以參考下2017-08-08
在PHP中靈活使用foreach+list處理多維數(shù)組的方法
這篇文章主要介紹了在PHP中靈活使用foreach+list處理多維數(shù)組的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Thinkphp使用Zxing擴(kuò)展庫解析二維碼內(nèi)容圖文講解
這篇文章主要介紹了Thinkphp使用Zxing擴(kuò)展庫解析二維碼內(nèi)容圖文講解,圖文步驟講解的很清晰,有需要的同學(xué)可以跟著小編一起來學(xué)習(xí)下2021-03-03

