Home > php教程 > php手册 > body text

URL、表单数据、IP等处理类 - 郑宏鑫erke

WBOY
Release: 2016-05-20 11:54:29
Original
978 people have browsed it
<span style="color: #000000;">php
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> ev
{
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> $cookie;
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> $post;
    </span><span style="color: #0000ff;">public</span> $<span style="color: #0000ff;">get</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> $file;
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> $url;
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> $G;
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> $e;

    </span><span style="color: #0000ff;">public</span> function __construct(<span style="color: #000000;">)
    {</span><span style="color: #0000ff;">if</span> (ini_get(<span style="color: #800000;">'</span><span style="color: #800000;">magic_quotes_gpc</span><span style="color: #800000;">'</span><span style="color: #000000;">)) {  //判断php.ini是否有<span style="color: #800000;">magic_quotes_gpc</span>过滤(转义)函数
            $</span><span style="color: #0000ff;">get</span>    = $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">stripSlashes($_REQUEST);
            $post   </span>= $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">stripSlashes($_POST);
            $</span><span style="color: #0000ff;">this</span>->cookie = $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">stripSlashes($_COOKIE);
        } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
            $</span><span style="color: #0000ff;">get</span>    =<span style="color: #000000;"> $_REQUEST;
            $post   </span>=<span style="color: #000000;"> $_POST;
            $</span><span style="color: #0000ff;">this</span>->cookie =<span style="color: #000000;"> $_COOKIE;
        }

        $</span><span style="color: #0000ff;">this</span>->file =<span style="color: #000000;"> $_FILES;
        $</span><span style="color: #0000ff;">this</span>-><span style="color: #0000ff;">get</span> = $<span style="color: #0000ff;">this</span>->initData($<span style="color: #0000ff;">get</span><span style="color: #000000;">);
        $</span><span style="color: #0000ff;">this</span>->post = $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">initData($post);
        $</span><span style="color: #0000ff;">this</span>->url = $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">parseUrl();
        $</span><span style="color: #0000ff;">this</span>->cookie = $<span style="color: #0000ff;">this</span>->initData($<span style="color: #0000ff;">this</span>-><span style="color: #000000;">cookie);
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">解析url</span>
    <span style="color: #0000ff;">public</span><span style="color: #000000;"> function parseUrl()
    {
        </span><span style="color: #0000ff;">if</span>(isset($_REQUEST[<span style="color: #800000;">'</span><span style="color: #800000;">route</span><span style="color: #800000;">'</span><span style="color: #000000;">]))
        {
            </span><span style="color: #0000ff;">return</span> explode(<span style="color: #800000;">'</span><span style="color: #800000;">-</span><span style="color: #800000;">'</span>,$_REQUEST[<span style="color: #800000;">'</span><span style="color: #800000;">route</span><span style="color: #800000;">'</span><span style="color: #000000;">]);  //解析路由
        }
        elseif(isset($_SERVER[</span><span style="color: #800000;">'</span><span style="color: #800000;">QUERY_STRING</span><span style="color: #800000;">'</span><span style="color: #000000;">]))
        {
            $tmp </span>= explode(<span style="color: #800000;">'</span><span style="color: #800000;">#</span><span style="color: #800000;">'</span>,$_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">QUERY_STRING</span><span style="color: #800000;">'</span>],<span style="color: #800080;">2</span><span style="color: #000000;">);
            $tp </span>= explode(<span style="color: #800000;">'</span><span style="color: #800000;">&</span><span style="color: #800000;">'</span>,$tmp[<span style="color: #800080;">0</span>],<span style="color: #800080;">2</span><span style="color: #000000;">);
            </span><span style="color: #0000ff;">return</span> explode(<span style="color: #800000;">'</span><span style="color: #800000;">-</span><span style="color: #800000;">'</span>,$tp[<span style="color: #800080;">0</span><span style="color: #000000;">]);
        }
        </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">返回$_REQUEST数组内的值</span>
    <span style="color: #0000ff;">public</span> function <span style="color: #0000ff;">get</span><span style="color: #000000;">($par)
    {
        </span><span style="color: #0000ff;">if</span>(isset($<span style="color: #0000ff;">this</span>-><span style="color: #0000ff;">get</span>[$par]))<span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">this</span>-><span style="color: #0000ff;">get</span><span style="color: #000000;">[$par];
        </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">返回$_POST数组内的值</span>
    <span style="color: #0000ff;">public</span><span style="color: #000000;"> function post($par)
    {
        </span><span style="color: #0000ff;">if</span>(isset($<span style="color: #0000ff;">this</span>->post[$par]))<span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">post[$par];
        </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">返回URL数组中的值</span>
    <span style="color: #0000ff;">public</span><span style="color: #000000;"> function url($par)
    {
        $par </span>=<span style="color: #000000;"> intval($par);
        </span><span style="color: #0000ff;">if</span>(isset($<span style="color: #0000ff;">this</span>->url[$par]))<span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">url[$par];
        </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">设置COOKIE</span>
    <span style="color: #0000ff;">public</span> function setCookie($name,$value,$time=<span style="color: #800080;">3600</span><span style="color: #000000;">)
    {
        </span><span style="color: #0000ff;">if</span>($time)$time = TIME +<span style="color: #000000;"> $time;
        </span><span style="color: #0000ff;">else</span> $time = <span style="color: #800080;">0</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(CDO)setCookie(CH.$name,$value,$time,CP,CDO);
        </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> setCookie(CH.$name,$value,$time,CP);
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">获取cookie</span>
    <span style="color: #0000ff;">public</span> function getCookie($par,$nohead = <span style="color: #800080;">0</span><span style="color: #000000;">)
    {
        </span><span style="color: #0000ff;">if</span>(isset($<span style="color: #0000ff;">this</span>->cookie[CH.$par]))<span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">cookie[CH.$par];
        elseif(isset($</span><span style="color: #0000ff;">this</span>->cookie[$par]) && $nohead)<span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">cookie[$par];
        </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">获取$_FILE</span>
    <span style="color: #0000ff;">public</span><span style="color: #000000;"> function getFile($par)
    {
        </span><span style="color: #0000ff;">if</span>(isset($<span style="color: #0000ff;">this</span>->file[$par]))<span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">file[$par];
        </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">初始化数据</span>
    <span style="color: #0000ff;">public</span><span style="color: #000000;"> function initData($data)
    {
        </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(is_array($data))
        {
            </span><span style="color: #0000ff;">foreach</span>($data <span style="color: #0000ff;">as</span> $key =><span style="color: #000000;"> $value)
            {
                </span><span style="color: #0000ff;">if</span>($<span style="color: #0000ff;">this</span>->strings->isAllowKey($key) === <span style="color: #0000ff;">false</span><span style="color: #000000;">)
                {
                    unset($data[$key]);
                }
                </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
                $data[$key] </span>= $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">initData($value);
            }
            </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $data;
        }
        </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
        {
            </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(is_numeric($data))
            {
                </span><span style="color: #0000ff;">if</span>($data[<span style="color: #800080;">0</span>] === <span style="color: #800080;">0</span>)<span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">this</span>->addSlashes(htmlspecialchars(str_replace(<span style="color: #800000;">"</span><span style="color: #800000;">'</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">'</span><span style="color: #800000;">"</span><span style="color: #000000;">,$data)));
                </span><span style="color: #0000ff;">if</span>(strlen($data) >= <span style="color: #800080;">11</span>)<span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">this</span>->addSlashes(htmlspecialchars(str_replace(<span style="color: #800000;">"</span><span style="color: #800000;">'</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">'</span><span style="color: #800000;">"</span><span style="color: #000000;">,$data)));
                </span><span style="color: #0000ff;">if</span>(strpos($data,<span style="color: #800000;">'</span><span style="color: #800000;">.</span><span style="color: #800000;">'</span>))<span style="color: #0000ff;">return</span><span style="color: #000000;"> floatval($data);
                </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> intval($data);
            }
            </span><span style="color: #0000ff;">if</span>(is_string($data))<span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">this</span>->addSlashes(htmlspecialchars(str_replace(<span style="color: #800000;">"</span><span style="color: #800000;">'</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">'</span><span style="color: #800000;">"</span><span style="color: #000000;">,$data)));
            </span><span style="color: #0000ff;">if</span>(is_bool($data))<span style="color: #0000ff;">return</span> (<span style="color: #0000ff;">bool</span><span style="color: #000000;">)$data;
            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        }
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">去除转义字符</span>
    <span style="color: #0000ff;">public</span><span style="color: #000000;"> function stripSlashes($data)
    {
        </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (is_array($data)) {
              </span><span style="color: #0000ff;">foreach</span> ($data <span style="color: #0000ff;">as</span> $key =><span style="color: #000000;"> $value) {
                $data[$key] </span>= $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">stripSlashes($value);
              }
        } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
              $data </span>=<span style="color: #000000;"> stripSlashes(trim($data));
        }

        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $data;
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">添加转义字符</span>
    <span style="color: #0000ff;">public</span><span style="color: #000000;"> function addSlashes($data)
    {
        </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (is_array($data)) {
              </span><span style="color: #0000ff;">foreach</span> ($data <span style="color: #0000ff;">as</span> $key =><span style="color: #000000;"> $value) {
                $data[$key] </span>= $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">addSlashes($value);
              }
        } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
              $data </span>=<span style="color: #000000;"> addSlashes(trim($data));
        }
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> $data;
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">获取客户端IP</span>
    <span style="color: #0000ff;">public</span><span style="color: #000000;"> function getClientIp()
    {
        </span><span style="color: #0000ff;">if</span>(!isset($<span style="color: #0000ff;">this</span>->e[<span style="color: #800000;">'</span><span style="color: #800000;">ip</span><span style="color: #800000;">'</span><span style="color: #000000;">]))
        {
            </span><span style="color: #0000ff;">if</span> (getenv(<span style="color: #800000;">"</span><span style="color: #800000;">HTTP_CLIENT_IP</span><span style="color: #800000;">"</span>) && strcasecmp(getenv(<span style="color: #800000;">"</span><span style="color: #800000;">HTTP_CLIENT_IP</span><span style="color: #800000;">"</span>), <span style="color: #800000;">"</span><span style="color: #800000;">unknown</span><span style="color: #800000;">"</span><span style="color: #000000;">))
                $ip </span>= getenv(<span style="color: #800000;">"</span><span style="color: #800000;">HTTP_CLIENT_IP</span><span style="color: #800000;">"</span><span style="color: #000000;">);
            </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (getenv(<span style="color: #800000;">"</span><span style="color: #800000;">HTTP_X_FORWARDED_FOR</span><span style="color: #800000;">"</span>) && strcasecmp(getenv(<span style="color: #800000;">"</span><span style="color: #800000;">HTTP_X_FORWARDED_FOR</span><span style="color: #800000;">"</span>), <span style="color: #800000;">"</span><span style="color: #800000;">unknown</span><span style="color: #800000;">"</span><span style="color: #000000;">))
                $ip </span>= getenv(<span style="color: #800000;">"</span><span style="color: #800000;">HTTP_X_FORWARDED_FOR</span><span style="color: #800000;">"</span><span style="color: #000000;">);
            </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (getenv(<span style="color: #800000;">"</span><span style="color: #800000;">REMOTE_ADDR</span><span style="color: #800000;">"</span>) && strcasecmp(getenv(<span style="color: #800000;">"</span><span style="color: #800000;">REMOTE_ADDR</span><span style="color: #800000;">"</span>), <span style="color: #800000;">"</span><span style="color: #800000;">unknown</span><span style="color: #800000;">"</span><span style="color: #000000;">))
                $ip </span>= getenv(<span style="color: #800000;">"</span><span style="color: #800000;">REMOTE_ADDR</span><span style="color: #800000;">"</span><span style="color: #000000;">);
            </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (isset($_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">REMOTE_ADDR</span><span style="color: #800000;">'</span>]) && $_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">REMOTE_ADDR</span><span style="color: #800000;">'</span>] && strcasecmp($_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">REMOTE_ADDR</span><span style="color: #800000;">'</span>], <span style="color: #800000;">"</span><span style="color: #800000;">unknown</span><span style="color: #800000;">"</span><span style="color: #000000;">))
                $ip </span>= $_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">REMOTE_ADDR</span><span style="color: #800000;">'</span><span style="color: #000000;">];
            </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
                $ip </span>= <span style="color: #800000;">"</span><span style="color: #800000;">unknown</span><span style="color: #800000;">"</span><span style="color: #000000;">;
            $</span><span style="color: #0000ff;">this</span>->e[<span style="color: #800000;">'</span><span style="color: #800000;">ip</span><span style="color: #800000;">'</span>] =<span style="color: #000000;"> $ip;
        }
        </span><span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">this</span>->e[<span style="color: #800000;">'</span><span style="color: #800000;">ip</span><span style="color: #800000;">'</span><span style="color: #000000;">];
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">根据二级域名获取信息</span>
    <span style="color: #0000ff;">public</span><span style="color: #000000;"> function getSecondDomain()
    {
        $domain </span>= $_SERVER[<span style="color: #800000;">'</span><span style="color: #800000;">HTTP_HOST</span><span style="color: #800000;">'</span><span style="color: #000000;">];
        $domain </span>= str_replace(array(<span style="color: #800000;">'</span><span style="color: #800000;">com.cn</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">net.cn</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">gov.cn</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">org.cn</span><span style="color: #800000;">'</span>),<span style="color: #800000;">'</span><span style="color: #800000;">com</span><span style="color: #800000;">'</span><span style="color: #000000;">,$domain);
        $tmp </span>= explode(<span style="color: #800000;">'</span><span style="color: #800000;">.</span><span style="color: #800000;">'</span><span style="color: #000000;">,$domain);
        </span><span style="color: #0000ff;">if</span>(count($tmp) 3)<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        elseif(is_numeric($tmp[</span><span style="color: #800080;">0</span>]))<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> $tmp[<span style="color: #800080;">0</span><span style="color: #000000;">];
    }
}
</span>?>
Copy after login

 

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template