欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

?PHP怎么防止XSS跨站腳本攻擊

這篇文章將為大家詳細講解有關PHP怎么防止XSS跨站腳本攻擊,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)建站是一家專業(yè)提供新沂企業(yè)網站建設,專注與成都網站設計、成都網站制作、HTML5、小程序制作等業(yè)務。10年已為新沂眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網站設計公司優(yōu)惠進行中。

PHP防止XSS跨站腳本攻擊的方法:是針對非法的HTML代碼包括單雙引號等,使用htmlspecialchars()函數(shù)。

在使用htmlspecialchars()函數(shù)的時候注意第二個參數(shù), 直接用htmlspecialchars($string) 的話,第二個參數(shù)默認是ENT_COMPAT,函數(shù)默認只是轉化雙引號(“), 不對單引號(‘)做轉義.

所以,htmlspecialchars函數(shù)更多的時候要加上第二個參數(shù), 應該這樣用: htmlspecialchars($string,ENT_QUOTES).當然,如果需要不轉化任何引號,用htmlspecialchars($string,ENT_NOQUOTES).

另外, 盡量少用htmlentities, 在全部英文的時候htmlentities和htmlspecialchars沒有區(qū)別,都可以達到目的.但是,中文情況下, htmlentities卻會轉化所有的html代碼,連同里面的它無法識別的中文字符也給轉化了。

htmlentities和htmlspecialchars這兩個函數(shù)對 '之類的字符串支持不好,都不能轉化, 所以用htmlentities和htmlspecialchars轉化的字符串只能防止XSS攻擊,不能防止SQL注入攻擊.

所有有打印的語句如echo,print等 在打印前都要使用htmlentities() 進行過濾,這樣可以防止Xss,注意中文要寫出htmlentities($name,ENT_NOQUOTES,GB2312) 。

(1).網頁不停地刷新 '<meta http-equiv="refresh" content="0;">'

(2).嵌入其它網站的鏈接 <iframe src=http://xxxx width=250 height=250></iframe>  除了通過正常途徑輸入XSS攻擊字符外,還可以繞過JavaScript校驗,通過修改請求達到XSS攻擊的目的.

<?php
//php防注入和XSS攻擊通用過濾
$_GET     && SafeFilter($_GET);
$_POST    && SafeFilter($_POST);
$_COOKIE  && SafeFilter($_COOKIE);
  
function SafeFilter (&$arr) 
{
   $ra=Array('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/','/script/','/javascript/','/vbscript/','/expression/','/applet/'
   ,'/meta/','/xml/','/blink/','/link/','/style/','/embed/','/object/','/frame/','/layer/','/title/','/bgsound/'
   ,'/base/','/onload/','/onunload/','/onchange/','/onsubmit/','/onreset/','/onselect/','/onblur/','/onfocus/',
   '/onabort/','/onkeydown/','/onkeypress/','/onkeyup/','/onclick/','/ondblclick/','/onmousedown/','/onmousemove/'
   ,'/onmouseout/','/onmouseover/','/onmouseup/','/onunload/');
     
   if (is_array($arr))
   {
     foreach ($arr as $key => $value) 
     {
        if (!is_array($value))
        {
          if (!get_magic_quotes_gpc())  //不對magic_quotes_gpc轉義過的字符使用addslashes(),避免雙重轉義。
          {
             $value  = addslashes($value); //給單引號(')、雙引號(")、反斜線(\)與 NUL(NULL 字符)
             #加上反斜線轉義
          }
          $value       = preg_replace($ra,'',$value);     //刪除非打印字符,粗暴式過濾xss可疑字符串
          $arr[$key]     = htmlentities(strip_tags($value)); //去除 HTML 和 PHP 標記并轉換為 HTML 實體
        }
        else
        {
          SafeFilter($arr[$key]);
        }
     }
   }
}
?>
$str = 'www.90boke.com<meta http-equiv="refresh" content="0;">';
SafeFilter ($str); //如果你把這個注釋掉,提交之后就會無休止刷新
echo $str;
//------------------------------php防注入和XSS攻擊通用過濾-----Start--------------------------------------------//
function string_remove_xss($html) {
    preg_match_all("/\<([^\<]+)\>/is", $html, $ms);
 
    $searchs[] = '<';
    $replaces[] = '<';
    $searchs[] = '>';
    $replaces[] = '>';
 
    if ($ms[1]) {
        $allowtags = 'img|a|font|div|table|tbody|caption|tr|td|th|br|p|b|strong|i|u|em|span|ol|ul|li|blockquote';
        $ms[1] = array_unique($ms[1]);
        foreach ($ms[1] as $value) {
            $searchs[] = "<".$value.">";
 
            $value = str_replace('&', '_uch_tmp_str_', $value);
            $value = string_htmlspecialchars($value);
            $value = str_replace('_uch_tmp_str_', '&', $value);
 
            $value = str_replace(array('\\', '/*'), array('.', '/.'), $value);
            $skipkeys = array('onabort','onactivate','onafterprint','onafterupdate','onbeforeactivate','onbeforecopy','onbeforecut','onbeforedeactivate',
                    'onbeforeeditfocus','onbeforepaste','onbeforeprint','onbeforeunload','onbeforeupdate','onblur','onbounce','oncellchange','onchange',
                    'onclick','oncontextmenu','oncontrolselect','oncopy','oncut','ondataavailable','ondatasetchanged','ondatasetcomplete','ondblclick',
                    'ondeactivate','ondrag','ondragend','ondragenter','ondragleave','ondragover','ondragstart','ondrop','onerror','onerrorupdate',
                    'onfilterchange','onfinish','onfocus','onfocusin','onfocusout','onhelp','onkeydown','onkeypress','onkeyup','onlayoutcomplete',
                    'onload','onlosecapture','onmousedown','onmouseenter','onmouseleave','onmousemove','onmouseout','onmouseover','onmouseup','onmousewheel',
                    'onmove','onmoveend','onmovestart','onpaste','onpropertychange','onreadystatechange','onreset','onresize','onresizeend','onresizestart',
                    'onrowenter','onrowexit','onrowsdelete','onrowsinserted','onscroll','onselect','onselectionchange','onselectstart','onstart','onstop',
                    'onsubmit','onunload','javascript','script','eval','behaviour','expression','style','class');
            $skipstr = implode('|', $skipkeys);
            $value = preg_replace(array("/($skipstr)/i"), '.', $value);
            if (!preg_match("/^[\/|\s]?($allowtags)(\s+|$)/is", $value)) {
                $value = '';
            }
            $replaces[] = empty($value) ? '' : "<" . str_replace('"', '"', $value) . ">";
        }
    }
    $html = str_replace($searchs, $replaces, $html);
 
    return $html;
}
//php防注入和XSS攻擊通用過濾 
function string_htmlspecialchars($string, $flags = null) {
    if (is_array($string)) {
        foreach ($string as $key => $val) {
            $string[$key] = string_htmlspecialchars($val, $flags);
        }
    } else {
        if ($flags === null) {
            $string = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string);
            if (strpos($string, '&#') !== false) {
                $string = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $string);
            }
        } else {
            if (PHP_VERSION < '5.4.0') {
                $string = htmlspecialchars($string, $flags);
            } else {
                if (!defined('CHARSET') || (strtolower(CHARSET) == 'utf-8')) {
                    $charset = 'UTF-8';
                } else {
                    $charset = 'ISO-8859-1';
                }
                $string = htmlspecialchars($string, $flags, $charset);
            }
        }
    }
 
    return $string;
}

//------------------php防注入和XSS攻擊通用過濾-----End--------------------------------------------//

PHP中的設置

PHP5.2以上版本已支持HttpOnly參數(shù)的設置,同樣也支持全局的HttpOnly的設置,在php.ini中

----------------------------------------------------- 
 session.cookie_httponly = 
-----------------------------------------------------

設置其值為1或者TRUE,來開啟全局的Cookie的HttpOnly屬性,當然也支持在代碼中來開啟:

<?php ini_set("session.cookie_httponly", 1);   
// or session_set_cookie_params(0, NULL, NULL, NULL, TRUE);   
?>

Cookie操作函數(shù)setcookie函數(shù)和setrawcookie函數(shù)也專門添加了第7個參數(shù)來做為HttpOnly的選項,開啟方法為:

<?php  
setcookie("abc", "test", NULL, NULL, NULL, NULL, TRUE);   
setrawcookie("abc", "test", NULL, NULL, NULL, NULL, TRUE);  
?>

關于PHP怎么防止XSS跨站腳本攻擊就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

分享文章:?PHP怎么防止XSS跨站腳本攻擊
本文鏈接:http://aaarwkj.com/article14/iipege.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供品牌網站建設標簽優(yōu)化、企業(yè)網站制作網站維護、手機網站建設App開發(fā)

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網站網頁設計
最新日韩精品电影在线网| 日产中文乱码字幕无线观看| 成人午夜性色福利视频| 欧美精品激情在线不卡| 亚洲一区二区三区日本久久| 国产免费很黄很色视频| 亚洲精品国产av成人| 99热免费精品在线观看| 97在线观看视频免费| 91熟女成人精品一区二区| 日韩综合欧美激情另类| 成熟人妻一区二区三区人妻| 国产免费看黄色的网站| 丰满少妇一区二区三区在线观看 | 蜜臀av在线播放黑丝| 国产传媒在线视频免费| 亚洲日日夜夜噜噜爽爽| 日本在线精品在线观看| 在线播放国产91精品| 国产乱子一区二区三区| 亚州欧美制服另类国产| 午夜激情在线观看网页| 婷婷六月开心激情五月| 欧美亚洲清纯唯美另类| 熟妇人妻中文字幕在线| 精品久久久久久久中文字幕| 男同午夜视频在线观看| 精品女同一区二区三区网站| 日本精品女优一区二区三区四区| 国产国产精品人在线观看| 中国亚洲视频一区二区| 国语对白视频在线观看| 久久精品国产精品日韩欧美 | 亚洲成人日韩欧美在线| 日韩成人大片在线播放| 老牛av一区二区三区| 精品国产乱码一区二区三区| 国产夫妻一区二区三区| 亚洲精品黄色在线观看| 国产国产乱老熟女视频网站| 精品一区二区亚洲精品|