How to implement header jump in php

WJ
Release: 2023-04-08 17:56:01
Original
5900 people have browsed it

How to implement header jump in php

How to implement header jump in php and precautions:

The header() function is a very useful method for page jump in PHP. simple way. The main function of the header() function is to output the HTTP protocol header (header) to the browser.

The header() function is defined as follows:

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


The optional parameter replace indicates whether to replace the previous similar header or add one of the same type (www.php.cn) Header, default to replace.

The second optional parameter http_response_code forces the HTTP corresponding code to the specified value. The Location type header in the header function is a special header call, often used to implement page jumps. Note: There cannot be a space between

1.location and the ":" sign, otherwise it will not jump.
2. There cannot be any output before using the header.
3.The PHP code after the header will also be executed. For example, redirect the browser to php.cn

<?php
 //重定向浏览器
header("Location: https://www.php.cn");
 //确保重定向后,后续代码不会被执行
exit;
?>
Copy after login

1. PHP jump code in one sentence:

<?php
$url = $_GET[&#39;url&#39;];
Header("Location:$url");
?>
Copy after login

2. PHP jump code if judgment:

Copy the code as follows:

if($_COOKIE["u_type"]){ 
header(&#39;location:register.php&#39;);
} else{ 
setcookie(&#39;u_type&#39;,&#39;1&#39;,&#39;86400*360&#39;);//设置cookie长期有效 
header(&#39;location:zc.html&#39;);
Copy after login

Note: Save it as zc.php. When the user visits zc.php, determine whether a cookie exists. If it exists (www.php .cn) will jump to register.php. If it does not exist, create a cookie and jump to zc.html.

URL redirection function

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=&#39;Refresh&#39; content=&#39;{$time};URL={$url}&#39;>”;
if ($time != 0)
 $str .= $msg;
 exit($str);
 }
 }
Copy after login

The above cannot return the 404 status. If the 404 status code is returned after the page jumps, we can do the following

function getref()
 {
 $url = @$_SERVER[&#39;HTTP_REFERER&#39;];
 if( !empty( $url ) )
 {
 if( !strstr($url ,&#39;jb51.net&#39; ) && !strstr($url,&#39;jb51.net&#39;))
 {
 @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();
 }
 }
Copy after login

If you want to do 301, it is almost the same

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

Related recommendations: php中文网

The above is the detailed content of How to implement header jump in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template