request_class.php
2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
 * Tiny - A PHP Framework For Web Artisans
 * @author Tiny <tinylofty@gmail.com>
 * @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
 * @version 1.0
 */
/**
 * 封装$_GET $_POST 的类,使$_GET $_POST 有一个统一的出入口
 * @class Req 
 * @note  
 */
class Req
{
    //对应处理$_GET
    public static function get()
    {
        $num = func_num_args();
        $args = func_get_args();
        if($num==1)
        {
            if(isset($_GET[$args[0]])){
                if(is_array($_GET[$args[0]]))return $_GET[$args[0]];
                else return trim($_GET[$args[0]]);
            }
            return null;
        }
        else if($num>=2)
        {
            if($args[1]!==null)$_GET[$args[0]] = $args[1];
            else if(isset($_GET[$args[0]])) unset($_GET[$args[0]]);
        }
        else
        {
            return $_GET;
        }
    }
    //对应处理$_POST
    public static function post()
    {
        $num = func_num_args();
        $args = func_get_args();
        if($num==1)
        {
            if(isset( $_POST[$args[0]])){
                if(is_array( $_POST[$args[0]]))return  $_POST[$args[0]];
                else return trim( $_POST[$args[0]]);
            }
            return null;
        }
        else if($num>=2)
        {
            if($args[1]!==null)	$_POST[$args[0]] = $args[1];
            else if(isset($_POST[$args[0]])) unset($_POST[$args[0]]);
        }
        else
        {
            return $_POST;
        }
    }
    //同时处理$_GET $_POST
    public static function args()
    {
        $num = func_num_args();
        $args = func_get_args();
        if($num==1)
        {
            if(isset($_POST[$args[0]])){
                if(is_array($_POST[$args[0]]))return $_POST[$args[0]];
                else return trim($_POST[$args[0]]);
            }
            else{
                if(isset($_GET[$args[0]])){
                    if(is_array($_GET[$args[0]]))return $_GET[$args[0]];
                    else return trim($_GET[$args[0]]);
                }
            }
            return null;
        }
        else if($num>=2)
        {
            if($args[1]!==null)
            {
                $_POST[$args[0]] = $args[1];
                $_GET[$args[0]] = $args[1];
            }
            else
            {
                if(isset($_GET[$args[0]])) unset($_GET[$args[0]]);
                if(isset($_POST[$args[0]])) unset($_POST[$args[0]]);
            }
        }
        else
        {
            return $_POST+$_GET;
        }
    }
    public function only()
    {
        $hash= md5(serialize($_POST));
        $safebox = Safebox::getInstance();
        $__hash__ = $safebox->get('__HASH__');
        if($hash != $__hash__)
        {
            $safebox->set('__HASH__',$hash);
            return true;
        }
        else
        {
            return false;
        }
    }
}
?>