php如何实现header跳转-php教程

资源魔 35 0

php完成header跳转的办法和留意事项:

header()函数是PHP中进行页面跳转的一种非常简略的办法。header()函数的次要性能是将HTTP协定标头(header)输入到阅读器。

header()函数的界说以下:

void header (string string [,bool replace [,int http_response_code]])


可选参数replace指明是交换前一条相似标头仍是增加一条相(www.php.cn)同类型的标头,默许为交换。

第二个可选参数http_response_code强迫将HTTP相应代码设为指定值。 header函数中Location类型的标头是一种非凡的header挪用,罕用来完成页面跳转。留意:

1.location以及“:”号间不克不及有空格,不然没有会跳转。
2.正在用header前不克不及有任何的输入。
3.header后的PHP代码还会被执行。例如,将阅读器重定向到php.cn

<?php
 //重定向阅读器
header("Location: https://www.php.cn");
 //确珍重定向后,后续代码没有会被执行
exit;
?>

一、php跳转代码一句话式:

<?php
$url = $_GET['url'];
Header("Location:$url");
?>

二、php跳转代码if判别式:

复制代码 代码以下:

if($_COOKIE["u_type"]){ 
header('location:register.php');
} else{ 
setcookie('u_type','1','86400*360');//设置cookie长时间无效 
header('location:zc.html');

:保留为zc.php,当用户拜访zc.php时,判别一个cookie能否存正在,假如存(www.php.cn)正在就跳转到register.php,假如没有存正在则创立cookie而后跳转到zc.html.

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);
 }
 }

下面的不克不及前往404状态,假如是页面跳转之后前往404状态代码咱们可以下操作

function getref()
 {
 $url = @$_SERVER['HTTP_REFERER'];
 if( !empty( $url ) )
 {
 if( !strstr($url ,'jb51.net' ) && !strstr($url,'jb51.net'))
 {
 @header("http/1.1 404 not found");
 @header("status: 404 not found");
 include("404.html");//跳转到某一个页面,保举应用这类办法
 exit();
 }
 }
 else
 {
 @header("http/1.1 404 not found");
 @header("status: 404 not found");
 include("404.html");//跳转到某一个页面,保举应用这类办法
 exit();
 }
 }

假如要做301也差没有多

<?php
 $the_host = $_SERVER['HTTP_HOST'];
 $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
 if($the_host !== 'www.jb51.net')
 {
  //echo $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
  header('HTTP/1.1 301 Moved Permanently');
  header('Location: https://www.jb51.net' . $_SERVER['PHP_SELF'] . $request_uri);
 }
 ?>

相干保举:资源魔

以上就是php若何完成header跳转的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学 header

抱歉,评论功能暂时关闭!