PHP封装的一个支持HTML、JS、PHP重定向的多功能跳转函数
PHP跳转,即重定向浏览器到指定的URL,是一个很常见的功能。这种功能也有一些细节性的要求,比如等待多少秒以后跳转,用不用JavaScript实现跳转,等等。下面的跳转方法考虑到很多,并参数化,可以用到具体的项目当中。
<?php /** *重定向浏览器到指定的URL * *@paramstring$url要重定向的url *@paramint$delay等待多少秒以后跳转 *@parambool$js指示是否返回用于跳转的JavaScript代码 *@parambool$jsWrapped指示返回JavaScript代码时是否使用<mce:scripttype="text/javascript"><!-- 标签进行包装 *@parambool$return指示是否返回生成的JavaScript代码 */ functionredirect($url,$delay=0,$js=false,$jsWrapped=true,$return=false) { $delay=(int)$delay; if(!$js){ if(headers_sent()||$delay>0){ echo<<<EOT <html> <head> <metahttp-equiv="refresh"content="{$delay};URL={$url}"/> </head> </html> EOT; exit; }else{ header("Location:{$url}"); exit; } } $out=''; if($jsWrapped){ $out.='<scriptlanguage="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; } ?>