URL重定向我们会使用到header函数来操作,最简单的就是直接使用header(‘Location: ‘ . $url);就可以了,如果要做像301定向我们还需要发送状态代码,下面整理了一些例子一起来看看吧。
代码如下 | 复制代码 | ||||||||
// URL重定向 function redirect($url, $time=0, $msg=”) { //多行URL地址支持 $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 = “”; if ($time != 0) $str .= $msg; exit($str); } } //url重定向2 function redirect($url) { echo “ “function redirect() {window.location.replace(‘$url’);}\n”. “setTimeout(‘redirect();’, 1000);\n”. “”; exit(); } 用HTTP头信息 也就是用PHP的HEADER函数。PHP里的HEADER函数的作用就是向浏览器发出由HTTP协议规定的本来应该通过WEB服务器的控制指令,例如声明返回信息的类型("Context-type: xxx/xxx"),页面的属性("No cache", "Expire")等等。 用HTTP头信息进行PHP重定向到另外一个页面的方法如下:
|