1. 程式人生 > >php後端對跳轉的封裝

php後端對跳轉的封裝


php後端對跳轉的封裝


/**
 * 頁面跳轉
 * $url 跳轉地址
 * $time 一段時間後跳轉
 */
function app_redirect($url,$time=0,$msg='')
{
    //多行URL地址支援
    $url = str_replace(array("\n", "\r"), '', $url);    
    if (!headers_sent()) {
        // redirect
        if(0===$time&&$msg=="") {
           if(substr($url,0,1)=="/")
           {             
              if(defined("SITE_DOMAIN"))
                 header("Location:".SITE_DOMAIN.$url);
              else
                 header("Location:".$url);
           }
           else
           {
              header("Location:".$url);
           }
            
        }else {
            header("refresh:{$time};url={$url}");   //$time 後跳轉 
            echo($msg);
        }
        exit();
    }else {
        $str    = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
        if($time!=0)
            $str   .=   $msg;
        exit($str);
    }
}