发新话题
打印

自动处理get、post提交内容的类

自动处理get、post提交内容的类

类代码如下:
复制内容到剪贴板
代码:
<?php
/*
* Yusi System for PHP5 Ver 1.0.4
* (C) Copyright 2006 Eyusi.com!
*  Author: enze   enzewei@gmail.com
* $Id: Yusi.Common.Class  2006/09/04 16:15 $
*/


class Common {
        
        private $value = '';
        private $key = '';
        private $val = '';
        private $array = array();
        private $tmpary = array();
        private $prefix = '';
        private $returnary = array();
        private $unset = false;
        
        function __construct()
        {
        }
        
        function __destruct()
        {
        }
        
        public function debug($var,$exit=0)
        {
                print "";
                print_r($var);
                print "";
                if($exit)
                {
                        exit;
                }
        }

        private function array_merge_user_func($myary,$type,$num)
        {
                reset($this->tmpary);
                if($num >= 4)
                {
                        $this->debug("参数非法",1);
                }
                switch($type)
                {
                        case 1:
                                $this->prefix='get_';
                        break;
                        case 2:
                                $this->prefix='post_';
                        break;
                }
                foreach ($myary as $this->key => $this->val)
                {
                        if(is_numeric($this->val))
                        {
                                $this->tmpary[$this->prefix.$this->key] = $this->getServer($this->key,1,$type);
                        }
                        else
                        {
                                if(!is_array($this->val))
                                {
                                        $this->tmpary[$this->prefix.$this->key] = $this->getServer($this->val,2,10);                                       
                                }
                                elseif(is_array($this->val))
                                {
                                        $num++;
                                        $this->array_merge_user_func($this->val,$type,$num);
                                }
                        }
                }
                return $this->tmpary;
        }
        
        public function autoInit()
        {
                if($_GET)
                {
                        $this->array = $this->array_merge_user_func($_GET,1,0);
                        if($this->unset)
                        {
                                unset($_GET);
                        }
                        reset($this->array);
                }
                if($_POST)
                {
                        $this->array = $this->array_merge_user_func($_POST,2,0);
                        if($this->unset)
                        {
                                unset($_POST);
                        }
                        reset($this->array);
                }
                return $this->array;
        }
        
        public function getServer($name,$type=1,$method=1)
        {
                switch($method)
                {
                        case 1:
                                @$this->value=$this->format($_GET[$name],$type);
                        break;
                        case 2:
                                @$this->value=$this->format($_POST[$name],$type);
                        break;
                        default:
                                @$this->value=$this->format($name,$type);
                        break;
                }
                return $this->value;
        }
        
        private function format($variable,$type,$filter=1)
        {
                switch($type)
                {
                        case 1:        //Int
                                $this->value=(int)$variable;
                        break;
                        default:        //Char
                                $this->value=(string)$this->html_t($variable);
                        break;
                }
                if($filter=1)
                {
                        $this->value=$this->filter_sql($this->value);
                }
                return $this->value;
        }
        
        public function t_html($variable)
        {
                $variable=str_replace('"','"',$variable);
                $variable=str_replace('<','<',$variable);
                $variable=str_replace('>','>',$variable);
                $variable=str_replace('&','&',$variable);
                $variable=str_replace(' '," ",$variable);
                $variable=str_replace('
',"\n",$variable);
                return $variable;
        }
        
        public function html_t($variable)
        {
                $variable=htmlspecialchars($variable);
                $variable=str_replace(" ",' ',$variable);
                $variable=str_replace("\n",'
',$variable);
                return $variable;
        }

        private function filter_sql($variable)
        {
                if (!get_magic_quotes_gpc())
                {
                        $variable=addslashes($variable);
                }
                return $variable;
        }

//Get Variable
        private function __get($name)
        {
                return $this->$name;
        }

//Set Variable
        private function __set($name,$value)
        {
                return $this->$name=$value;
        }
}
?>
使用方法:
复制内容到剪贴板
代码:
$cls = new Common();
Extract($cls->autoInit(),EXTR_SKIP);
如果有GET提交过来的一律采用如下形式表示
复制内容到剪贴板
代码:
$get_var
如果有POST提交过来一律采用如下形式表示
复制内容到剪贴板
代码:
$post_var
重点:
如果是数字形式,请不要超过10位数,因为在PHP中处理的时候会溢出!切记,切记!
广告位招租。

TOP

TOP

不错,顶起来

TOP

发新话题