ホームページ >php教程 >php手册 >URL、表单数据、IP等处理类,url表单

URL、表单数据、IP等处理类,url表单

WBOY
WBOYオリジナル
2016-06-13 08:43:15968ブラウズ

URL、表单数据、IP等处理类,url表单

<?<span>php
</span><span>class</span><span> ev
{
    </span><span>public</span><span> $cookie;
    </span><span>public</span><span> $post;
    </span><span>public</span> $<span>get</span><span>;
    </span><span>public</span><span> $file;
    </span><span>public</span><span> $url;
    </span><span>public</span><span> $G;
    </span><span>private</span><span> $e;

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

        $</span><span>this</span>->file =<span> $_FILES;
        $</span><span>this</span>-><span>get</span> = $<span>this</span>->initData($<span>get</span><span>);
        $</span><span>this</span>->post = $<span>this</span>-><span>initData($post);
        $</span><span>this</span>->url = $<span>this</span>-><span>parseUrl();
        $</span><span>this</span>->cookie = $<span>this</span>->initData($<span>this</span>-><span>cookie);
    }

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

    </span><span>//</span><span>返回$_REQUEST数组内的值</span>
    <span>public</span> function <span>get</span><span>($par)
    {
        </span><span>if</span>(isset($<span>this</span>-><span>get</span>[$par]))<span>return</span> $<span>this</span>-><span>get</span><span>[$par];
        </span><span>else</span> <span>return</span> <span>false</span><span>;
    }

    </span><span>//</span><span>返回$_POST数组内的值</span>
    <span>public</span><span> function post($par)
    {
        </span><span>if</span>(isset($<span>this</span>->post[$par]))<span>return</span> $<span>this</span>-><span>post[$par];
        </span><span>else</span> <span>return</span> <span>false</span><span>;
    }

    </span><span>//</span><span>返回URL数组中的值</span>
    <span>public</span><span> function url($par)
    {
        $par </span>=<span> intval($par);
        </span><span>if</span>(isset($<span>this</span>->url[$par]))<span>return</span> $<span>this</span>-><span>url[$par];
        </span><span>else</span> <span>return</span> <span>false</span><span>;
    }

    </span><span>//</span><span>设置COOKIE</span>
    <span>public</span> function setCookie($name,$value,$time=<span>3600</span><span>)
    {
        </span><span>if</span>($time)$time = TIME +<span> $time;
        </span><span>else</span> $time = <span>0</span><span>;
        </span><span>if</span><span>(CDO)setCookie(CH.$name,$value,$time,CP,CDO);
        </span><span>else</span><span> setCookie(CH.$name,$value,$time,CP);
    }

    </span><span>//</span><span>获取cookie</span>
    <span>public</span> function getCookie($par,$nohead = <span>0</span><span>)
    {
        </span><span>if</span>(isset($<span>this</span>->cookie[CH.$par]))<span>return</span> $<span>this</span>-><span>cookie[CH.$par];
        elseif(isset($</span><span>this</span>->cookie[$par]) && $nohead)<span>return</span> $<span>this</span>-><span>cookie[$par];
        </span><span>else</span> <span>return</span> <span>false</span><span>;
    }

    </span><span>//</span><span>获取$_FILE</span>
    <span>public</span><span> function getFile($par)
    {
        </span><span>if</span>(isset($<span>this</span>->file[$par]))<span>return</span> $<span>this</span>-><span>file[$par];
        </span><span>else</span> <span>return</span> <span>false</span><span>;
    }

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

    </span><span>//</span><span>去除转义字符</span>
    <span>public</span><span> function stripSlashes($data)
    {
        </span><span>if</span><span> (is_array($data)) {
              </span><span>foreach</span> ($data <span>as</span> $key =><span> $value) {
                $data[$key] </span>= $<span>this</span>-><span>stripSlashes($value);
              }
        } </span><span>else</span><span> {
              $data </span>=<span> stripSlashes(trim($data));
        }

        </span><span>return</span><span> $data;
    }

    </span><span>//</span><span>添加转义字符</span>
    <span>public</span><span> function addSlashes($data)
    {
        </span><span>if</span><span> (is_array($data)) {
              </span><span>foreach</span> ($data <span>as</span> $key =><span> $value) {
                $data[$key] </span>= $<span>this</span>-><span>addSlashes($value);
              }
        } </span><span>else</span><span> {
              $data </span>=<span> addSlashes(trim($data));
        }
        </span><span>return</span><span> $data;
    }

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

    </span><span>//</span><span>根据二级域名获取信息</span>
    <span>public</span><span> function getSecondDomain()
    {
        $domain </span>= $_SERVER[<span>'</span><span>HTTP_HOST</span><span>'</span><span>];
        $domain </span>= str_replace(array(<span>'</span><span>com.cn</span><span>'</span>,<span>'</span><span>net.cn</span><span>'</span>,<span>'</span><span>gov.cn</span><span>'</span>,<span>'</span><span>org.cn</span><span>'</span>),<span>'</span><span>com</span><span>'</span><span>,$domain);
        $tmp </span>= explode(<span>'</span><span>.</span><span>'</span><span>,$domain);
        </span><span>if</span>(count($tmp) < <span>3</span>)<span>return</span> <span>false</span><span>;
        elseif(is_numeric($tmp[</span><span>0</span>]))<span>return</span> <span>false</span><span>;
        </span><span>else</span> <span>return</span> $tmp[<span>0</span><span>];
    }
}
</span>?>

 

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。