php的URL重新導向header()函數

來源:互聯網
上載者:User

// 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 “

”;

exit();

}

用HTTP頭資訊

也就是用PHP的HEADER函數。PHP裡的HEADER函數的作用就是向瀏覽器發出由HTTP協議規定的本來應該通過WEB伺服器的控制指令,例如聲明返回資訊的類型("Context-type: xxx/xxx"),頁面的屬性("No cache", "Expire")等等。

用HTTP頭資訊進行PHP重新導向到另外一個頁面的方法如下:

<?php

$url = "www.111cn.net";

if (!empty($url))

{

Header("HTTP/1.1 303 See Other"); //這條語句可以不寫

Header("Location: $url");

}

?>

注意一下,"Localtion:"後面有一個空格,下面整理了一個全面的函數。

/**

* 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;

}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.