zoukankan      html  css  js  c++  java
  • php过滤字符串函数

    <?php
    class request
    {
        public  function __construct()
        {
            if(!get_magic_quotes_gpc())
            {
                if(!empty($_POST))
                {
                    foreach ($_POST as $k => &$v)
                    {
                        if(is_array($v))
                        {
                            @array_walk($v, 'urldecode');
                            @array_walk($v, 'addslashes');
                        }
                        else
                        {
                            $v = addslashes(urldecode($v));
                        }
                        $p[$k] = $v;
                    }
                    $_POST = $p;
                    unset($p);
                }
                if(!empty($_GET))
                {
                    foreach ($_GET as $k => &$v)
                    {
                        if(is_array($v))
                        {
                            @array_walk($v, 'urldecode');
                            @array_walk($v, 'addslashes');
                        }
                        else
                        {
                            $v = addslashes(urldecode($v));
                        }
                        $g[$k] = $v;
                    }
                    $_GET = $g;
                    unset($g);
                }
            }
        }
        public static function getQuery( $key )
        {
            if( isset( $_GET[$key]) )
            {
                return self::xss_clean($_GET[$key]);
            }
            else
            {
                return false;
            }
        }
        
        public static function getPost( $key )
        {
            if( isset( $_POST[$key]) )
            {
                return self::xss_clean($_POST[$key]);
            }
            else
            {
                return false;
            }        
        }
        public static function getServer($key)
        {
            $key = strtoupper($key);
            if(isset($_SERVER[$key]))
            {
                return self::xss_clean($_SERVER[$key]);
            }
            return false;
        }
        
        public static function  getSession( $key )
        {
            if( isset( $_SESSION[$key]) )
            {
                return self::xss_clean($_SESSION[$key]);
            }
            else
            {
                return false;
            }        
        }    
        public static function getCookie( $key )
        {
            if( isset( $_COOKIE[$key]) )
            {
                return $_COOKIE[$key];
            }
            else
            {
                return false;
            }        
        }
            /**
         * 过滤非法字符(分发)
         */
        private static function xss_clean($str) {
            if (is_array($str) && !empty($str)) {
                $str = self::xss_clean_arr($str);
            } else {
                $str = self::xss_clean_str($str);
            }
            return $str;
        }

        /**
         * 过滤非法字符(数组)
         */
        private static function xss_clean_arr($str) {
            foreach ($str as $key => $val) {
                if (is_array($val)) {
                    $val = self::xss_clean_arr($val);
                } else {
                    $val = self::xss_clean_str($val);
                }
                $arr[$key] = $val;
            }
            return $arr;
        }

        /**
         * 过滤非法字符(字符串)
         */
        private static function xss_clean_str($str) {
            $str = preg_replace('#(alert|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(s*)((.*?))#si', "\1\2&#40;\3&#41;", $str);
            if (get_magic_quotes_gpc()) {
                return $str;
            } else {
                return addslashes($str);
            }
        }    
    }

  • 相关阅读:
    vuecli 4使用report分析vendor.js
    vue使用 NProgress 浏览器顶部进度条
    vue项目中 configureWebpack 与 chainWebpack的区别及配置方式
    vue 项目中报错 Error: Avoided redundant navigation to current location: “/xxx”. 的解决方案
    npm中的savedev和save的区别
    vuecli 4 使用scss (配置全局scss变量)
    css如何修改滚动条样式
    vue 项目http://localhost:8080/sockjsnode/info?t=1556418283950 net:: ERR_CONNECTION_REFUSED
    java类的加载时机
    android中屏蔽键盘的2种方法
  • 原文地址:https://www.cnblogs.com/kwishly/p/3529999.html
Copyright © 2011-2022 走看看