Home > Article > Backend Development > How to jump in php header
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');".
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['url']; Header("Location:$url"); ?>
2, PHP jump code if judgment:
The code is as follows:
if($_COOKIE["u_type"]){ header('location:register.php'); } else{ setcookie('u_type','1','86400*360');//设置cookie长期有效 header('location:zc.html');
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='Refresh' content='{$time};URL={$url}'>”; 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['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(); } }
If you want to do 301, it is almost the same
<?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); } ?>
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!