I was asked by a visitor how he could "use PHP to redirect to another web page". This article answers that question.
Please note that this article specifically answers the question of how to use PHP for the redirection. It is useful if you are writing a PHP script that needs to direct a user to some other address after it's done some task, or as part of its operation.
If you merely want to send someone who arrives at an obsolete address at your website to a new one, you should read How to Redirect a URL instead. The latter method is more efficient for the general case since the web server doesn't need to run a PHP interpreter just to do a redirection.
Redirection is trivial with PHP. All you need to do is to send the visitor's browser an HTTP header specifying
the new location, and the browser will do the rest. This is done with the header()
function.
When used to send a location (as in the "Location: https://www.thesitewizard.com/" shown here), the header
function also sends a "302 Found" (or "302 Moved temporarily") status code to the web browser. On receipt of these,
the browser will typically load the new URL specified, which, in this case, is "https://www.thesitewizard.com/".
Note that header()
does not automatically terminate the script, since the PHP interpreter does not know
the meaning of any header you send. As such, if you have more code following that line (for example, the
header()
call occurs in an if
block or in a function that will return to the main program),
you should remember to call exit
to end processing. For the benefit of newcomers to PHP,
who may not know what I mean when I say "remember to call exit
", the above example superfluously
invokes exit
, even though it is not needed here, since the demo script doesn't do anything else
but quits immediately.
If you want maximum compatibility with old browsers, use an absolute URL for the redirection. That is, specify the web address in full, including the protocol (eg, "https://").
If you want to signal a permanent redirection, that is, by returning a "301 Moved Permanently" status code, use something like the following:
The second parameter, "TRUE", tells PHP this header is to replace a previous similar header, and "301" is the actual status code for a permanent redirection. Note that although "TRUE" is the default for the second parameter, you must nonetheless supply it if you are planning to specify a third argument (the "301" status code). In my first code example earlier, I omitted both the second and third arguments, so the default values of TRUE and 302 were used. However, if you want to change 302 to something else, you must specify all 3 parameters.
Headers must always be the first output sent to a browser. If your script sends any
content before calling header()
, you will probably get a
"Warning: Cannot modify header information - headers already sent" error message (or the like)
when you run the script.
If you are a newcomer to PHP, it may not be obvious to you that normal content has already been sent to the browser. Take the following buggy script as an example.
Although the script looks correct, PHP will spew an error message. That's because there
is a blank line before "<?php
". When the interpreter sees the blank line,
it will send it to the browser as content. Under the HTTP protocol, headers (containing technical information
for the browser) must come before content (the part of the page meant for the user). Once content is sent,
the browser no longer expects any more headers, and anything sent to it will be regarded as part of a web page
(or image or video or some other type of content). But by the time the PHP interpreter encounters the
header()
instruction in the above script, it is too late. The blank line has already been sent.
If you want to send a redirection header (or any header, for that matter) in a PHP script, make sure no content is sent before that point in time.
Copyright © 2019 Christopher Heng. All rights reserved.
Get more free tips and articles like this,
on web design, promotion, revenue and scripting, from https://www.thesitewizard.com/.
Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at https://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.
This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.
It will appear on your page as:
How to Redirect to Another Web Page in PHP