亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Laravel5中防止XSS跨站攻擊的方法

 更新時(shí)間:2016年10月10日 14:29:47   作者:swteen  
這篇文章主要介紹了Laravel5中防止XSS跨站攻擊的方法,結(jié)合實(shí)例形式分析了Laravel5基于Purifier擴(kuò)展包集成HTMLPurifier防止XSS跨站攻擊的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Laravel5中防止XSS跨站攻擊的方法。分享給大家供大家參考,具體如下:

Laravel 5本身沒有這個(gè)能力來防止xss跨站攻擊了,但是這它可以使用Purifier 擴(kuò)展包集成 HTMLPurifier 防止 XSS 跨站攻擊。

1、安裝

HTMLPurifier 是基于 PHP 編寫的富文本 HTML 過濾器,通常我們可以使用它來防止 XSS 跨站攻擊,更多關(guān)于 HTMLPurifier的詳情請(qǐng)參考其官網(wǎng):http://htmlpurifier.org/。Purifier 是在 Laravel 5 中集成 HTMLPurifier 的擴(kuò)展包,我們可以通過 Composer 來安裝這個(gè)擴(kuò)展包:

composer require mews/purifier

安裝完成后,在配置文件config/app.php的providers中注冊(cè)HTMLPurifier服務(wù)提供者:

'providers' => [
 // ...
 Mews\Purifier\PurifierServiceProvider::class,
]
然后在aliases中注冊(cè)Purifier門面:
'aliases' => [
 // ...
 'Purifier' => Mews\Purifier\Facades\Purifier::class,
]

2、配置

要使用自定義的配置,發(fā)布配置文件到config目錄:

php artisan vendor:publish

這樣會(huì)在config目錄下生成一個(gè)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)行動(dòng)態(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); //如果你把這個(gè)注釋掉,你就知道xss攻擊的厲害了
echo $str;
?>

更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 2個(gè)Codeigniter文件批量上傳控制器寫法例子

    2個(gè)Codeigniter文件批量上傳控制器寫法例子

    這篇文章主要介紹了2個(gè)Codeigniter文件批量上傳控制器寫法例子,需要的朋友可以參考下
    2014-07-07
  • PHP explode()函數(shù)的幾個(gè)應(yīng)用和implode()函數(shù)有什么區(qū)別

    PHP explode()函數(shù)的幾個(gè)應(yīng)用和implode()函數(shù)有什么區(qū)別

    這篇文章主要介紹了PHP explode()函數(shù)的幾個(gè)應(yīng)用和implode()函數(shù)有什么區(qū)別,需要的朋友可以參考下
    2015-11-11
  • PHP利用Cookie設(shè)置用戶30分鐘未操作自動(dòng)退出功能

    PHP利用Cookie設(shè)置用戶30分鐘未操作自動(dòng)退出功能

    這篇文章主要介紹了PHP利用Cookie設(shè)置用戶30分鐘未操作自動(dòng)退出功能,需要的朋友可以參考下
    2017-07-07
  • PHP如何批量修改二維數(shù)組中值(五種方案)

    PHP如何批量修改二維數(shù)組中值(五種方案)

    這篇文章主要介紹了PHP如何批量修改二維數(shù)組中值,本文給大家分享五種解決方案,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2023-10-10
  • laravel修改用戶模塊的密碼驗(yàn)證實(shí)現(xiàn)

    laravel修改用戶模塊的密碼驗(yàn)證實(shí)現(xiàn)

    本文主要介紹了laravel修改用戶模塊的密碼驗(yàn)證實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • laravel withCount 統(tǒng)計(jì)關(guān)聯(lián)數(shù)量的方法

    laravel withCount 統(tǒng)計(jì)關(guān)聯(lián)數(shù)量的方法

    今天小編就為大家分享一篇laravel withCount 統(tǒng)計(jì)關(guān)聯(lián)數(shù)量的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • 最新評(píng)論