ThinkPHP源码学习 redirect函数 URL重定向 - web开发

/**
 * URL重定向
 * @param string $url 重定向的URL地址
 * @param integer $time 重定向的等待时间(秒)
 * @param string $msg 重定向前的提示信息
 * @return void
 */
 $url='http://www.baidu.com';
 $time=3;
function redirect($url, $time=0, $msg='') {
//多行URL地址支持
    $url        = str_replace(array("\n", "\r"), '', $url);// $url字符串中换行符\n 回车符\r替换为空
//给出提示信息
      if (empty($msg))
        $msg    = "系统将在{$time}秒之后自动跳转到{$url}!";
//headers_sent — Checks if or where headers have been sent
    if (headers_sent()) {//headers_sent未发送时为false
// redirect
        if (0 === $time) {
            header('Location: ' . $url);//Location定位
        } else {
          header("refresh:{$time};url={$url}");//refresh重新刷新
            echo($msg);
        }
        exit();
    } else {//headers_sent已发送情况 html页面下定时刷新
        $str    = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
        if ($time != 0)
            $str .= $msg;
        exit($str);
    }
}

ThinkPHP源码学习 redirect函数  URL重定向 - web开发

赞(0)
未经允许不得转载:主机测评网 » ThinkPHP源码学习 redirect函数 URL重定向 - web开发
分享到: 更多 (0)