中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

前端如何防止xss和sql注入

小新
343
2020-12-28 18:02:20
欄目: 云計算

前端如何防止xss和sql注入

前端防止xss和sql注入的方法:

使用php語言進行預防xss和sql注入,代碼如下:

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防sql注入和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;

}

0
怀远县| 镇平县| 哈密市| 当涂县| 逊克县| 香港| 探索| 司法| 彭水| 康定县| 黑水县| 多伦县| 饶阳县| 尉氏县| 祥云县| 阿拉善盟| 合作市| 平定县| 英山县| 华蓥市| 民丰县| 吉林市| 喀喇沁旗| 沁阳市| 德安县| 泉州市| 诸暨市| 崇左市| 台江县| 礼泉县| 天峨县| 修水县| 栾城县| 丰镇市| 太湖县| 嘉黎县| 无锡市| 罗城| 周宁县| 正定县| 兴国县|