PHP小函數集-篇一

1、 驗證spa

 

 

    /**
     * 判斷用戶名是否規範
     */
    function is_username($username)
    {
        if (preg_match("/^[a-zA-Z]{1}([0-9a-zA-Z]|[._]){3,19}$/",$username))
        {
            return true;
        }
    }

 

    /**
     * 判斷密碼是否規範
     */
    function is_password($password)
    {
        if (preg_match("/^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,22}$/",$password))
        {
            return true;
        }
    }

 

    /**
     * 判斷是否爲數字
     */
    function is_number($number)
    {
        if (preg_match("/^[0-9]*[1-9][0-9]*$/", $number))
        {
            return true;
        }
    }

 

    /**
     * 判斷是否爲郵件地址
     */
    function is_email($email)
    {
        if (preg_match("/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/", $email))
        {
            return true;
        }
    }

 

    /**
     * 判斷是否爲字母
     */
    function is_letter($letter)
    {
        if (preg_match("/^[a-z]+$/", $letter))
        {
            return true;
        }
    }

 

    /**
     * 驗證文本域輸入的內容
     */
    function is_text($text)
    {
        if (preg_match("/^[\x{4e00}-\x{9fa5}a-zA-Z0-9,,。.??!!*-_\s]+$/u", $text))
        {
            return true;
        }
    }

 

    /**
     * 驗證文本框輸入的內容
     */
    function is_textarea($textarea)
    {
        if (preg_match("/^[\x{4e00}-\x{9fa5}a-zA-Z0-9,,。.??!!*-_@=#\s\n\r]+$/u", $textarea))
        {
            return true;
        }
    }

 

 

    /**
     * 判斷 文件/目錄 是否可寫
     */
    function check_writeable($file)
    {
        if (file_exists($file))
        { 
            if (is_dir($file))
            {
                $dir = $file;
                if ($fp = @fopen("$dir/test.txt", 'w'))
                {
                    @fclose($fp);
                    @unlink("$dir/test.txt");
                    $writeable = 1;
                }
                else
                {
                    $writeable = 0;
                }
            }
            else
            {
                if ($fp = @fopen($file, 'a+'))
                {
                    @fclose($fp);
                    $writeable = 1;
                }
                else
                {
                    $writeable = 0;
                }
            }
        }
        else
        {
            $writeable = 2;
        }
    
        return $writeable;
    }

 

    /**
     +----------------------------------------------------------
     * 遞歸方式的對變量中的特殊字符進行轉義
     +----------------------------------------------------------
     */
    function addslashes_deep($value)
    {
        if (empty ($value))
        {
            return $value;
        }

        if (is_array($value))
        {
            foreach ((array) $value as $k => $v)
            {
                unset ($value[$k]);
                $k = addslashes($k);
                if (is_array($v))
                    $value[$k] = addslashes_deep($v);
                else
                    $value[$k] = addslashes($v);
            }
        }
        else
        {
            $value = addslashes($value);
        }
        return $value;
    }

    /**
     +----------------------------------------------------------
     * 遞歸方式的對變量中的特殊字符去除轉義
     +----------------------------------------------------------
     */
    function stripslashes_deep($value)
    {
        if (empty ($value))
        {
            return $value;
        }

        if (is_array($value))
        {
            foreach ((array) $value as $k => $v)
            {
                unset ($value[$k]);
                $k = stripslashes($k);
                if (is_array($v))
                {
                    $value[$k] = stripslashes_deep($v);
                }
                else
                {
                    $value[$k] = stripslashes($v);
                }
            }
        }
        else
        {
            $value = stripslashes($value);
        }
        return $value;
    }

    /**
     +----------------------------------------------------------
     * 交互數據轉義操做
     +----------------------------------------------------------
     */
    function dou_magic_quotes()
    {
        if (!@ get_magic_quotes_gpc())
        {
            if (!empty ($_GET))
                $_GET = addslashes_deep($_GET);
            if (!empty ($_POST))
                $_POST = addslashes_deep($_POST);
            $_COOKIE = addslashes_deep($_COOKIE);
            $_REQUEST = addslashes_deep($_REQUEST);
        }
    }

 

    /**
     +----------------------------------------------------------
     * 獲取真實IP地址
     +----------------------------------------------------------
     */
    function get_ip()
    {
        $ip = false;
        if (!empty ($_SERVER["HTTP_CLIENT_IP"]))
        {
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
        if (!empty ($_SERVER['HTTP_X_FORWARDED_FOR']))
        {
            $ips = explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
            if ($ip)
            {
                array_unshift($ips, $ip);
                $ip = FALSE;
            }
            for ($i = 0; $i < count($ips); $i++)
            {
                if (!preg_match("/^(10|172\.16|192\.168)\./", $ips[$i]))
                {
                    $ip = $ips[$i];
                    break;
                }
            }
        }
        return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
    }
相關文章
相關標籤/搜索