Home  >  Article  >  Backend Development  >  How to jump in php header

How to jump in php header

藏色散人
藏色散人Original
2022-12-02 09:14:347287browse

How to implement jump in php header: 1. Use "Header("Location:$url");" syntax to implement jump; 2. Use if judgment to implement jump, and its jump statement is such as " if($_COOKIE["u_type"]){ header('location:register.php'); } else{ setcookie('u_type','1','86400*360');".

How to jump in php header

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to jump the php header?

Summary of several issues that should be paid attention to when PHP Header is used for page jumps

Use header("location:test.php") in PHP Please pay attention to the following points when jumping, which will help solve some problems often encountered by novices

1.header() function

The header() function is a part of page jump in PHP A very simple method. 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]])

The optional parameter replace specifies whether to replace the previous similar header or add a header of the same type (www.jb51.net). Default is 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 jb51.net

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

1, PHP jump code in one sentence:

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

2, PHP jump code if judgment:

The code is 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;);

Note: Save it as zc.php. When the user accesses zc.php, it will determine whether a cookie exists. If it exists (www.jb51.net), it will jump to register.php , if it does not exist, create a cookie and jump to zc.htmlfrom:https://www.jb51.net/phper/php-cy/62883.htm

URL redirection function

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

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

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

Recommended learning: "PHP Video tutorial

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

Statement:
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