php 跳转,即重定向浏览器到指定的 url,是一个很常见的功能。这种功能也有一些细节性的要求,比如等待多少秒以后跳转,用不用javascript实现跳转,等等。下面的跳转方法考虑到很多,并参数化,可以用到具体的项目当中。
<?php
/**
* 重定向浏览器到指定的 url
*
* @param string $url 要重定向的 url
* @param int $delay 等待多少秒以后跳转
* @param bool $js 指示是否返回用于跳转的 javascript 代码
* @param bool $jswrapped 指示返回 javascript 代码时是否使用 <mce:script type="text/javascript"><!--
标签进行包装
* @param bool $return 指示是否返回生成的 javascript 代码
*/
function redirect($url, $delay = 0, $js = false, $jswrapped = true, $return = false)
{
$delay = (int)$delay;
if (!$js) {
if (headers_sent() || $delay > 0) {
echo <<<eot
<html>
<head>
<meta http-equiv="refresh" content="{$delay};url={$url}" />
</head>
</html>
eot;
exit;
} else {
header("location: {$url}");
exit;
}
}
$out = '';
if ($jswrapped) {
$out .= '<script language="javascript" type="text/javascript">';
}
$url = rawurlencode($url);
if ($delay > 0) {
$out .= "window.settimeout(function () { document.location='{$url}'; }, {$delay});";
} else {
$out .= "document.location='{$url}';";
}
if ($jswrapped) {
$out .= '
// --></mce:script>';
}
if ($return) {
return $out;
}
echo $out;
exit;
}
?>
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!