Using PHP's header() function, you can achieve page jumps, such as
<code>1 |
<code>header(<code>"Location: "<code>. <code>$url<code>); |
But it has a disadvantage, once the HTTP header block has been sent, you cannot use the header() function to send other headers.
At this time, you can only use front-end HTML or JS technology to achieve page jumps!
How to know that the HTTP header block has been sent?
PHP’s headers_sent() function can help.
PHP headers_sent() function
headers_sent() function checks if and where HTTP headers have been sent.
Returns true if the header has been sent, false otherwise.
Syntax
headers_sent(file,line)
Parameters |
Description |
file,line |
Optional. If you set the file and line parameters, headers_sent() will store the PHP source file name and line number where the output starts into the file and line variables. |
Tips and Notes
Note: Once the header block has been sent, you cannot use the header() function to send additional headers. Using this function can at least avoid error messages related to HTTP headers.
Note: The optional file and line parameters are new in PHP 4.3.
Example 1
<code>2 | <code> // If header is not sent, send one |
<code>3 | <code>if<code>(!headers_sent()) { |
<code>4 | <code><code>header(<code>" Location: http://www.w3school.com.cn/"<code>); |
<code>5 | <code> <code><code> |
6
7
?><code> |
<code>
|
Example 2
Use optional file and line Parameters:
01
02
// Pass $file and $line for later use<code> | <code> |
03
// Don’t pre-assign them <code> | <code> |
04
if<code>(!headers_sent( | $ file<code>,
<code>$line<code>)) {<code><code><code> |
05
) <code>header( | "Location: http://www.w3school. com.cn/"<code> );<code><code><code> |
06
<code> | <code>07<code><code> | // Trigger an error here
<code> | 08<code><code>} | else
{
<code> "Headers sent in" $file on line $line " | ;<code><code><code> |
10
<code> | <code><code>11<code><code> } |
<code>12 | <code>?><code><code>
|
Based on the above knowledge points, we can organize Create your own PHP jump function:
<code>02 |
<code> |
* URL jump
03 <code>
|
<code>* @param string $url Jump address |
<code>04 |
<code><code>* @param int $time Jump delay (unit : seconds)
|
<code>05 |
<code> <code>* @param string $msg prompt
|
<code>07 |
<code>function<code>redirect(<code>$url<code>,
<code>$time<code>= 0, <code>$msg<code>= <code>''<code>) {
|
<code>08 |
<code> <code>$url<code>= <code> str_replace<code>(<code>array<code>(<code>"n"<code>,
<code>"r"<code>), <code>''<code>,
... (<code>empty<code>(<code>$msg |
)) {
<code> |
10<code><code> $msg<code>= <code>"The system will be in {$time} Automatically jump to {$url} after seconds! "<code>;<code><code>
|
11
<code> |
<code>12<code><code> <code>if<code>(headers_sent() ) { |
<code>14 |
<code> <code><code>
| 15
.= $msg<code>; |
<code><code><code><code><code>16
|
<code> |
<code>17<code><code> exit<code>(<code>$str
| );
<code> | 18
<code><code> <code>} <code>else <code>{
|
<code> 19 |
<code> <code> |
.
$url
);
<code> |
<code><code>21<code><code> else<code>{ |
<code>22 ErHeader ( |
"Content-Type: Text/HTML; Charset = UTF-8" <code>); <code><code><code>
|
23
<code>Header ( |
"refresh:{$time};url={$url}"<code>);<code><code><code><code>
|
24
cho
(<code>$msg |
);<code><code><code><code><code><code>25 |
26
<code> |
<code><code><code>27<code>
|
}
<code> |
<code>28<code> <code>}<code>
|
The above introduces the PHP redirection web page, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.