Home > Backend Development > PHP Tutorial > What are the methods for commonly used url address jumps in PHP?

What are the methods for commonly used url address jumps in PHP?

伊谢尔伦
Release: 2023-03-11 08:50:01
Original
5685 people have browsed it

No matter which method you use to jump URLs in php, you cannot do without the header function. Below I will sort out some commonly used procedures and methods for implementing URL jumps. Friends in need can refer to them.

1.header() function

The header() function is a very simple method for page jump in PHP. 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. The default is 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: http://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

Note: If saved as ad.php, ad.php can be implemented ?url=www.111cn.net The effect of jumping to Jisi.net
2. PHP jump code if judgment:

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 as zc.php, when the user visits zc.php When, determine whether a cookie exists, if it exists, jump to register.php, if it does not exist, create a cookie and jump to zc.html

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);
}
}
Copy after login

above The 404 status cannot be returned. 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;111cn.net&#39; ) && !strstr($url,&#39;111cn.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

The above is the detailed content of What are the methods for commonly used url address jumps 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