Home >php教程 >php手册 >php通用全局安全过滤xss & 防注入

php通用全局安全过滤xss & 防注入

WBOY
WBOYOriginal
2016-06-06 19:39:341483browse

php防注入过滤xss ?php//php防注入和XSS攻击通用过滤. //by qq:831937$_GET SafeFilter($_GET);$_POST SafeFilter($_POST);$_COOKIE SafeFilter($_COOKIE); function SafeFilter ($arr) { if (is_array($arr)) { foreach ($arr as $key = $value) { if (!is_

php 防注入 过滤xss

<?php
//php防注入和XSS攻击通用过滤.  
//by qq:831937
$_GET		&& SafeFilter($_GET);
$_POST		&& SafeFilter($_POST);
$_COOKIE	&& SafeFilter($_COOKIE);
 
function SafeFilter (&$arr) 
{   
      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 字符)加上反斜线转义
                    }
                    $arr[$key]         = htmlspecialchars($value,ENT_QUOTES);	//&,",',> ,< 转为html实体 &,"&#039;,>,<

               }
               else
               {
                    SafeFilter($arr[$key]);
               }
          }
     }
}
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn