Php перенаправление на другую страницу php. Как в PHP задать редирект на другой URL до загрузки страницы

(PHP 4, PHP 5, PHP 7)

header — Send a raw HTTP header

Description

header (string $header [, bool $replace = TRUE [, int $http_response_code ]]) : void

header() is used to send a raw HTTP header. See the » HTTP/1.1 specification for more information on HTTP headers.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include , or require , functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.


Parameters

The header string.

There are two special-case header calls. The first is a header that starts with the string "HTTP/ " (case is not significant), which will be used to figure out the HTTP status code to send. For example, if you have configured Apache to use a PHP script to handle requests for missing files (using the ErrorDocument directive), you may want to make sure that your script generates the proper status code.

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

Replace

The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in FALSE as the second argument you can force multiple headers of the same type. For example:

Http_response_code

Forces the HTTP response code to the specified value. Note that this parameter only has an effect if the header is not empty.

Return Values

No value is returned.

Changelog Version Description
5.1.2 This function now prevents more than one header to be sent at once as a protection against header injection attacks.
Examples

Example #1 Download dialog

If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.