Home > Article > Backend Development > PHP function header() implements page jump
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: //m.sbmmt.com"); //确保重定向后,后续代码不会被执行 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=//m.sbmmt.com">
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='javascript' type='text/javascript'>"; echo "window.location.href='//m.sbmmt.com'"; echo "</script>"; ?>
More PHP functions header() to achieve page jump For related articles, please pay attention to the PHP Chinese website!