Home  >  Article  >  Backend Development  >  PHP function header() implements page jump

PHP function header() implements page jump

高洛峰
高洛峰Original
2016-12-20 11:00:053003browse

There are multiple methods for page jump. First, we will introduce the implementation method of using the built-in functions in PHP, mainly using the header() function. The main function of the
header() function is to output the HTTP protocol header (header) to the browser.

Syntax

header(string,replace,http_response_code)
Parameter description string required. Specifies the header string to be sent. replace is optional. Indicates whether this header replaces the previous header, or adds a second header. Default is true (replacement). false (allow multiple headers of the same type). http_response_code is optional. Forces the HTTP response code to the specified value. (Available in PHP 4 and above)

Tips and Notes

Note: From PHP 4.4 onwards, this function prevents multiple headers from being sent at once. This is a protection measure against header injection attacks.

Specific code:

< ?php
//重定向浏览器
header("Location: http://www.php.cn");
//确保重定向后,后续代码不会被执行
exit;
?>
< ?php
//重定向到news.php页面
header("Location: news.php");
//确保重定向后,后续代码不会被执行
exit;
?>

Notes:
This function needs to change the PHP.INI file to take effect. Find the php.ini configuration file, then search for one item: output_buffering, change its value from off to on, and restart Apache.

Appendix (two other jump implementation methods):

Method 1: Use the Meta tag

The Meta tag is a tag in HTML responsible for providing document meta-information. Using this tag in a PHP program can also achieve page jumps change. If http-equiv is defined as refresh, when the page is opened, it will jump to the corresponding page within a certain period of time based on the value specified by content.

If content="seconds;url=website" is set, it defines how long it takes for the page to jump to the specified URL. For example, use the meta tag to automatically jump to the Guanwei blog after the vaccine.

< meta http-equiv="refresh" content="1;url=http://www.php.cn">

For example, the following program meta.php implements the page to automatically jump to the ph Chinese website after staying on the page for one second

Method 2: Use JS

< ?php
echo "<script language=&#39;javascript&#39; type=&#39;text/javascript&#39;>";
echo "window.location.href=&#39;http://www.php.cn&#39;";
echo "</script>";
?>

More PHP functions header() to achieve page jump For related articles, please pay attention to 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