Home > Article > Backend Development > What are the methods for commonly used url address jumps in PHP?
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; ?>
1. PHP jump code in one sentence:
<?php $url = $_GET['url']; Header("Location:$url"); ?>
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('location:register.php'); } else{ setcookie('u_type','1','86400*360');//设置cookie长期有效 header('location:zc.html'); }
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); } }
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['HTTP_REFERER']; if( !empty( $url ) ) { if( !strstr($url ,'111cn.net' ) && !strstr($url,'111cn.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(); } }
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!