本文实例总结了php常用工具函数。分享给大家供大家参考,具体如下:
移除xss攻击脚本
function removexss($val) { // remove all non-printable characters. cr(0a) and lf(0b) and tab(9) are allowed // this prevents some character re-spacing such as <javafunction removexss($val) {// remove all non-printable characters. cr(0a) and lf(0b) and tab(9) are allowed// this prevents some character re-spacing such as <java\0script>// note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs$val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val);// straight replacements, the ur should never need the since they're normal characters// this prevents like <img src=@avascript:alert('xss')>$arch = 'abcdefghijklmnopqrstuvwxyz';$arch .= 'abcdefghijklmnopqrstuvwxyz';$arch .= '1234567890!@#$%^&*()';$arch .= '~`";:?+/={}[]-_|\窃书记'\\';for ($i = 0; $i < strlen($arch); $i++) {// ;? matches the ;, which is optional// 0{0,7} matches any padded zeros, which are optional and go up to 8 chars// @ @ arch for the hex values$val = preg_replace('/(&#[xx]0{0,8}'.dechex(ord($arch[$i])).';?)/i', $arch[$i], $val); // with a ;// @ @ 0{0,7} matches '0' zero to ven times$val = preg_replace('/({0,8}'.ord($arch[$i]).';?)/', $arch[$i], $val); // with a ;}// now the only remaining whitespace attacks are \t, \n, and \r$ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'framet', 'ilayer', 'layer', 'bgsound', 'title', 'ba');$ra2 = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrollect', 'oncopy', 'oncut', 'ondataavailable', 'ondatatchanged', 'ondatatcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlocapture', 'onmoudown', 'onmouenter', 'onmouleave', 'onmoumove', 'onmouout', 'onmouover', 'onmouup', 'onmouwheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onret', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinrted', 'onscroll', 'onlect', 'onlectionchange', 'onlectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');$ra = array_merge($ra1, $ra2);$found = true; // keep replacing as long as the previous round replaced somethingwhile ($found == true) {$val_before = $val;for ($i = 0; $i < sizeof($ra); $i++) {$pattern = '/';for ($j = 0; $j < strlen($ra[$i]); $j++) {if ($j > 0) {$pattern .= '(';$pattern .= '(&#[xx]0{0,8}([9ab]);)';$pattern .= '|';$pattern .= '|({0,8}([9|10|13]);)';$pattern .= ')*';}$pattern .= $ra[$i][$j];}$pattern .= '/i';$replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag$val = preg_replace($pattern, $replacement, $val); // filter out the hex tagsif ($val_before == $val) {// no replacements were made, so exit the loop$found = fal;}}}return $val;}script> // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs $val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val); // straight replacements, the ur should never need the since they're normal characters // this prevents like <img src=@avascript:alert('xss')> $arch = 'abcdefghijklmnopqrstuvwxyz'; $arch .= 'abcdefghijklmnopqrstuvwxyz'; $arch .= '1234567890!@#$%^&*()'; $arch .= '~`";:?+/={}[]-_|\'\\'; for ($i = 0; $i < strlen($arch); $i++) { // ;? matches the ;, which is optional // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars // @ @ arch for the hex values $val = preg_replace('/(&#[xx]0{0,8}'.dechex(ord($arch[$i])).';?)/i', $arch[$i], $val); // with a ; // @ @ 0{0,7} matches '0' zero to ven times $val = preg_replace('/({0,8}'.ord($arch[$i]).';?)/', $arch[$i], $val); // with a ; } // now the only remaining whitespace attacks are \t, \n, and \r $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'fra狗为什么不能吃巧克力met', 'ilayer', 'layer', 'bgsound', 'title', 'ba'); $ra2 = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange'打假办, 'onclick', 'oncontextmenu', 'oncontrollect', 'oncopy', 'oncut', 'ondataavailable', 'ondatatchanged', 'ondatatcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlocapture', 'onmoudown', 'onmouenter', 'onmouleave', 'onmoumove', 'onmouout', 'onmouover', 'onmouup', 'onmouwheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechang世界黑社会老大排行榜e', 'onret', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinrted', 'onscroll', 'onlect', 'onlectionchange', 'onlectstart', 'onstart', 'onstop', 'onsubmit', 'onunload'); $ra = array_merge($ra1, $ra2); $found = true; // keep replacing as long as the previous round replaced something while ($found == true) { $val_before = $val; for ($i = 0; $i < sizeof($ra); $i++) { $pattern = '/'; for ($j = 0; $j < strlen($ra[$i]); $j++) { if ($j > 0) { $pattern .= '('; $pattern .= '(&#[xx]0{0,8}([9ab]);)'; $pattern .= '|'; $pattern .= '|({0,8}([9|10|13]);)'; $pattern .= ')*'; } $pattern .= $ra[$i][$j]; } $pattern .= '/i'; $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag $val = preg_replace($pattern, $replacement, $val); // fil西塞山怀古 ter out the hex tags if ($val_before == $val) { // no replacements were made, so exit the loop $found = fal; } } } return $val;}
gbk转utf8
function gbktoutf8($str){ if(is_array($str)) { foreach ($str as &$value) { $value = gbktoutf8($value); } return $str; }elif(is_string($str)){ $str = iconv("gb18030", "utf-8//ignore", $str); return $str; }el{ return $str; }}
utf8转gbk
function utf8togbk(&$str){ if(is_array($str)) { foreach ($str as &$value) { $value = utf8togbk($value); } return $str; }elif (is_string($str)){ $str = iconv("utf-8", "gb18030//ignore", $str); return $str; }el{ return $str; }}
更多关于php相关内容感兴趣的读者可查看本站专题:《php编码与转码操作技巧汇总》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php常用函数与技巧总结》及《php错误与异常处理方法总结》
希望本文所述对大家php程序设计有所帮助。
本文发布于:2023-04-07 09:00:56,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/1510f9c65efb7801147abfec85c14bdb.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:PHP常用工具函数小结【移除XSS攻击、UTF8与GBK编码转换等】.doc
本文 PDF 下载地址:PHP常用工具函数小结【移除XSS攻击、UTF8与GBK编码转换等】.pdf
留言与评论(共有 0 条评论) |