function redirect($url, $time=0, $msg='') {
//多行URL地址支持把其中的\n,\r删掉
$url = str_replace(array("\n", "\r"), '', $url);
if (empty($msg))
$msg = "系统将在{$time}秒之后自动跳转到{$url}!";
if (!headers_sent()) {
// redirect
if (0 === $time) {
header("Location: " . $url);
} else {
header("refresh:{$time};url={$url}");
echo($msg);
}
exit();
} else {
$str = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
if ($time != 0)
$str .= $msg;
exit($str);
}
}
str_replace函数:
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
If search and replace are arrays, then str_replace() takes a value from each array and uses them to do search and replace on subject . If replace has fewer values than search , then an empty string is used for the rest of replacement values. If search is an array and replace is a string, then this replacement string is used for every value of search . The converse would not make sense, though.
If search or replace are arrays, their elements are processed first to last.
(如果参数search和replace都是数组,那么str_replace()将对search和replace中的值一一对应进行对subject的替换。如果replace数组中值的个数比search少,那么剩余的值会默认为空值。(PS:如果replace数组中的值的个数比search多,多余的将被忽略。)
如果search是数组而且replace是一个字符串(String),那么替换的String将被用来进行search中所有的值的替换。尽管反过来是不行的。
如果search或者replace是数组,他们的值都是从头到尾的进行对应替换)
如果 HTTP 标头尚未被发送出去的话,headers_sent() 将返回 FALSE,否则返回 TRUE,通过header发送,否则通过meta标签跳转。
PHP中的跳转:(来自百度文库,有改动)
1、header()函数是PHP中进行页面跳转的一种十分简单的方法。header()函数的主要功能是将HTTP协议标头(header)输出到浏览器。
<?php
header("Location: http://www.baidu.com");
exit;//确保重定向后,后面的代码不会被执行
?>
2、Meta标签
meta标签是HTML中负责提供文档元信息的标签,在PHP程序中使用该标签,也可以实现页面的跳转。弱定义http_equiv为fresh,则打开面的时候将根据content规定的值在一定时间内跳转到相应页面。
<meta http-equiv="refesh" content="秒数";url=http://www.baidu.com>
3、javascipt实现跳转
window.location.href=http://www.baidu.com;