教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php的URL重定向header()函数

php的URL重定向header()函数

发布时间:2016-10-24   编辑:jiaochengji.com
教程集为您提供php的URL重定向header()函数等资源,欢迎您收藏本站,我们将为您提供最新的php的URL重定向header()函数资源
URL重定向我们会使用到header函数来操作,最简单的就是直接使用header(‘Location: ‘ . $url);就可以了,如果要做像301定向我们还需要发送状态代码,下面整理了一些例子一起来看看吧。
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3972')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3972>


// 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 = “<meta http-equiv=’Refresh’ content=’{$time};URL={$url}’>”;
if ($time != 0)
$str .= $msg;
exit($str);
}
}
 
//url重定向2

function redirect($url) {
echo “<script>”.
“function redirect() {window.location.replace(‘$url’);}\n”.
“setTimeout(‘redirect();’, 1000);\n”.
“</script>”;
exit();
}

用HTTP头信息

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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7651')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7651><?php
$url = "www.jiaochengji.com"; 
if (!empty($url))   
{   
    Header("HTTP/1.1 303 See Other"); //这条语句可以不写 
    Header("Location: $url"); 
}   
?>   

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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7838')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7838>

/**
 * get_redirect_url()
 * Gets the address that the provided URL redirects to,
 * or FALSE if there's no redirect.
 *
 * @param string $url
 * @return string
 */
function get_redirect_url($url){
 $redirect_url = null;
 
 $url_parts = @parse_url($url);
 if (!$url_parts) return false;
 if (!isset($url_parts['host'])) return false; //can't process relative URLs
 if (!isset($url_parts['path'])) $url_parts['path'] = '/';
 
 $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30);
 if (!$sock) return false;
 
 $request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1rn";
 $request .= 'Host: ' . $url_parts['host'] . "rn";
 $request .= "Connection: Closernrn";
 fwrite($sock, $request);
 $response = '';
 while(!feof($sock)) $response .= fread($sock, 8192);
 fclose($sock);
 
 if (preg_match('/^Location: (. ?)$/m', $response, $matches)){
  return trim($matches[1]);
 } else {
  return false;
 }
 
}
 
/**
 * get_all_redirects()
 * Follows and collects all redirects, in order, for the given URL.
 *
 * @param string $url
 * @return array
 */
function get_all_redirects($url){
 $redirects = array();
 while ($newurl = get_redirect_url($url)){
  if (in_array($newurl, $redirects)){
   break;
  }
  $redirects[] = $newurl;
  $url = $newurl;
 }
 return $redirects;
}
 
/**
 * get_final_url()
 * Gets the address that the URL ultimately leads to.
 * Returns $url itself if it isn't a redirect.
 *
 * @param string $url
 * @return string
 */
function get_final_url($url){
 $redirects = get_all_redirects($url);
 if (count($redirects)>0){
  return array_pop($redirects);
 } else {
  return $url;
 }
}

您可能感兴趣的文章:
利用php header函数实现文件下载保存到本地
php页面跳转函数 页面重定向
php中url重定向的方法
php header的作用
有关php页面重定向的三种方法
PHP Header 页面跳转注意事项
php中header函数的用法举例详解
php如何重定向页面跳转?
php如何实现页面跳转
IIS 301重定向设置

[关闭]
~ ~