首页 > 语言 > PHP > 正文

php的URL重定向header()函数

2024-09-04 11:48:28
字体:大 中 小
来源:转载
供稿:网友

URL重定向我们会使用到header函数来操作,最简单的就是直接使用header(‘Location: ‘ . $url);就可以了,如果要做像301定向我们还需要发送状态代码,下面整理了一些例子一起来看看吧,代码如下:

  1. // URL重定向 
  2. function redirect($url, $time=0, $msg=”) { 
  3. //多行URL地址支持 
  4. $url = str_replace(array(“\n”, “\r”), ”, $url); 
  5. if ( emptyempty($msg) ) 
  6. $msg = “系统将在{$time}秒之后自动跳转到{$url}!”; 
  7. if (!headers_sent()) { 
  8. // redirect 
  9. if (0 === $time) { 
  10. header(‘Location: ‘ . $url); 
  11. } else { 
  12. header(“refresh:{$time};url={$url}”); 
  13. echo($msg); 
  14. } 
  15. exit(); 
  16. } else { 
  17. $str = “<meta http-equiv=’Refresh’ content=’{$time};URL={$url}’>”; 
  18. if ($time != 0) 
  19. $str .= $msg; 
  20. exit($str); 
  21. } 
  22. } 
  23. //url重定向2 
  24. function redirect($url) { 
  25. echo “<script>”. 
  26. “function redirect() {window.location.replace(‘$url’);}\n”. 
  27. “setTimeout(‘redirect();’, 1000);\n”. 
  28. “</script>”; 
  29. exit(); 
  30. } 

用HTTP头信息

也就是用PHP的HEADER函数。PHP里的HEADER函数的作用就是向浏览器发出由HTTP协议规定的本来应该通过WEB服务器的控制指令,例如声明返回信息的类型("Context-type: xxx/xxx"),页面的属性("No cache", "Expire")等等。

用HTTP头信息进行PHP重定向到另外一个页面的方法,代码如下:

  1. <?php  
  2. $url = "www.Vevb.com";   
  3. if (!emptyempty($url))     
  4. {     
  5.     Header("HTTP/1.1 303 See Other"); //这条语句可以不写   
  6.     Header("Location: $url");   
  7. }     
  8. ?>    

注意一下,"Localtion:"后面有一个空格,下面整理了一个全面的函数,代码如下:

  1. /** 
  2.  * get_redirect_url() 
  3.  * Gets the address that the provided URL redirects to, 
  4.  * or FALSE if there's no redirect.  
  5.  * 
  6.  * @param string $url 
  7.  * @return string 
  8.  */ 
  9. function get_redirect_url($url){ 
  10.  $redirect_url = null;  
  11.  
  12.  $url_parts = @parse_url($url); 
  13.  if (!$url_parts) return false; 
  14.  if (!isset($url_parts['host'])) return false; //can't process relative URLs 
  15.  if (!isset($url_parts['path'])) $url_parts['path'] = '/'; 
  16.  
  17.  $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30); 
  18.  if (!$sock) return false; 
  19.  
  20.  $request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1rn";  
  21.  $request .= 'Host: ' . $url_parts['host'] . "rn";  
  22.  $request .= "Connection: Closernrn";  
  23.  fwrite($sock, $request); 
  24.  $response = ''; 
  25.  while(!feof($sock)) $response .= fread($sock, 8192); 
  26.  fclose($sock); 
  27.  
  28.  if (preg_match('/^Location: (.+?)$/m', $response, $matches)){ 
  29.   return trim($matches[1]); 
  30.  } else { 
  31.   return false; 
  32.  } 
  33.  
  34. } 
  35.  
  36. /** 
  37.  * get_all_redirects() 
  38.  * Follows and collects all redirects, in order, for the given URL.  
  39.  * 
  40.  * @param string $url 
  41.  * @return array 
  42.  */ 
  43. function get_all_redirects($url){ 
  44.  $redirects = array(); 
  45.  while ($newurl = get_redirect_url($url)){ 
  46.   if (in_array($newurl, $redirects)){ 
  47.    break; 
  48.   } 
  49.   $redirects[] = $newurl; 
  50.   $url = $newurl; 
  51.  } 
  52.  return $redirects; 
  53. } 
  54.  
  55. /** 
  56.  * get_final_url() 
  57.  * Gets the address that the URL ultimately leads to.  
  58.  * Returns $url itself if it isn't a redirect. 
  59.  * 
  60.  * @param string $url 
  61.  * @return string 
  62.  */ 
  63. function get_final_url($url){ 
  64.  $redirects = get_all_redirects($url); 
  65.  if (count($redirects)>0){ 
  66.   return array_pop($redirects); 
  67.  } else { 
  68.   return $url; 
  69.  } 
  70. } 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表