PHP commonly used URL address jump What is the method?

Source: Internet
Author: User
In PHP URL jump regardless of that approach can not be separated from the header function, below I will give you some commonly used URL jump implementation procedures and methods, there is a need for friends to refer to.

1.header () function

The header () function is a very simple way to jump a page 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, indicates whether to replace the previous similar header or to add a header of the same type, which is replaced by default.

The Second optional parameter, Http_response_code, forces the HTTP corresponding code to be set to the specified value. The header of the location type in the header function is a special header call that is commonly used to implement page jumps. Attention:

1.location and ":" Can not have spaces between, otherwise will not jump.

2. There can be no output before using the header.

PHP code after 3.header will also be executed. For example, redirect the browser to php.cn

< PHP//redirect Browser header ("location:http://www.php.cn"); Ensure that subsequent code is not executed after redirection; >

1, PHP Jump code a sentence:

<?php $url = $_get[' url ']; Header ("Location: $url");?>

Note: If saved as ad.php, you can realize the effect of ad.php?url=www.111cn.net jump to the set of network.
2, PHP jump code if judgment:

if ($_cookie["U_type"]) {header (' location:register.php ');} else{Setcookie (' U_type ', ' 1 ', ' 86400*360 ');// Set the cookie long-term valid header (' location:zc.html '); }

Note: Save as zc.php, when the user accesses zc.php, determine whether a cookie exists, jump to register.php if it exists, create a cookie if it does not exist, and then jump to zc.html

URL redirection function

URL redirection function Redirect ($url, $time =0, $msg = ") {//multiline URL address is supported by the Str_replace (array (" n "," R "),", $url); if (Empty ($msg ) $msg = "The system will automatically jump to {$url}! after {$time} seconds if (!headers_sent ()) {//redirectif (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 can not return 404 status, if it is a page jump after returning 404 status Code We can do the following

function getref () {$url = @$_server[' Http_referer ']; if (!empty ($url)) {if (!str   STR ($url, ' 111cn.net ') &&!strstr ($url, ' 111cn.net ')) {@header ("http/1.1 404 Not Found");   @header ("status:404 not Found");  Include ("404.html");//Jump to a page, it is recommended to use this method exit ();  }} else {@header ("http/1.1 404 Not Found");  @header ("status:404 not Found"); Include ("404.html");//Jump to a page, it is recommended to use this method exit (); }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.