php redirect web page

WBOY
Release: 2016-07-30 13:30:38
Original
1387 people have browsed it

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>1 <code><?php

<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

}<code><code>

7

Example 2
?><code> <code>
Use optional file and line Parameters:

01

<?php<code><code>

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

// Trigger an error here
<code><code>07<code><code>​​​

08else{
<code><code><code>}

;
<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>01 <code>/**

* URL jump
<code>02 <code>

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>06 <code><code>*/

<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>13 <code>                                                   <code>;

15
<code>14 <code>                                                                                                <code><code>

.= $msg
<code>; <code><code><code><code><code>16

);
<code> <code>17<code><code> exit<code>(<code>$str

18
<code> <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>
}

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.
<code> <code>28<code> <code>}<code>

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!