Yii分析6:CHttpRequest

官方说明:CHttpRequest封装了$_SERVER变量,同时解决了不同web服务器之间一致性的问题。

/**
     * 返回当前请求的路径.
     * 头尾的斜线会被去除掉
     * 注意从1.1.4开始pathinfo被编码
     * 在1.1.4之前, 是否被编码取决于服务器
     * (大部分情况未编码).
     */
    public function getPathInfo()
    {  
        if($this->_pathInfo===null)
        {  
            $pathInfo=$this->getRequestUri();

            if(($pos=strpos($pathInfo,'?'))!==false)
               $pathInfo=substr($pathInfo,0,$pos);

            $pathInfo=urldecode($pathInfo);

            $scriptUrl=$this->getScriptUrl();
            $baseUrl=$this->getBaseUrl();
            if(strpos($pathInfo,$scriptUrl)===0)
                $pathInfo=substr($pathInfo,strlen($scriptUrl));
            else if($baseUrl==='' || strpos($pathInfo,$baseUrl)===0)
                $pathInfo=substr($pathInfo,strlen($baseUrl));
            else if(strpos($_SERVER['PHP_SELF'],$scriptUrl)===0)
                $pathInfo=substr($_SERVER['PHP_SELF'],strlen($scriptUrl));
            else
                throw new CException(Yii::t('yii','CHttpRequest is unable to determine the path info of the request.'));

            $this->_pathInfo=trim($pathInfo,'/');
        }  
        return $this->_pathInfo;
}
/**
     * 返回请求URI
     * 指的是跟在host info后面的部分
     * 包含了 query string
     * 这个方法的实现参考了Zend框架中的Zend_Controller_Request_Http.
     * @return string the request URI portion for the currently requested URL.
     * @throws CException if the request URI cannot be determined due to improper server configuration
     * @since 1.0.1
     */
    public function getRequestUri()
    {
        if($this->_requestUri===null)
        {
            if(isset($_SERVER['HTTP_X_REWRITE_URL'])) // IIS
                $this->_requestUri=$_SERVER['HTTP_X_REWRITE_URL'];
            else if(isset($_SERVER['REQUEST_URI']))
            {
                $this->_requestUri=$_SERVER['REQUEST_URI'];
                if(isset($_SERVER['HTTP_HOST']))//来自header
                {
                    if(strpos($this->_requestUri,$_SERVER['HTTP_HOST'])!==false)
                        $this->_requestUri=preg_replace('/^\w+:\/\/[^\/]+/','',$this->_requestUri);
                }
                else
                    $this->_requestUri=preg_replace('/^(http|https):\/\/[^\/]+/i','',$this->_requestUri);
            }
            else if(isset($_SERVER['ORIG_PATH_INFO']))  // IIS 5.0 CGI
            {
                $this->_requestUri=$_SERVER['ORIG_PATH_INFO'];
                if(!empty($_SERVER['QUERY_STRING']))
                    $this->_requestUri.='?'.$_SERVER['QUERY_STRING'];
            }
            else
                throw new CException(Yii::t('yii','CHttpRequest is unable to determine the request URI.'));
        }

        return $this->_requestUri;
    }
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值