2013-04-17

php相對網址轉絕對網址


相對網址轉絕對網址




function convUrl($url){

        $URL_Info = parse_url($url);
        if(isset($URL_Info['scheme']))return $url;
        if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
            $output = 'https://';
        } else {
            $output = 'http://';
        }
        $url = str_replace('\\','/',$url);
        if(!isset($URL_Info['host'])){
            if($url{0} == '/'){
                $output .= getenv('HTTP_HOST');
            }else{
                $pnum = substr_count($url,'../');
                $url = str_replace('../','',$url);
                $path = dirname(getenv('SCRIPT_NAME'));
                for($i = 0 ; $i < $pnum; $i++){
                    $path = substr($path,0,strrpos($path,'/'));
                }
                $output .= getenv('HTTP_HOST')
                    .$path
                    .'/'
                ;
               
            }
        }
        return $output.$url;
    }

沒有留言:

張貼留言