php的URL重定向header()函数

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