PHP Redirecting with the header function and Location

October 21, 2011 | By Regaz | Filed in: Uncategorized.

The PHP 5 header function is used to Send a raw Http header.
If it Content begin with the “Location:” keyword it Result in a 302 Redirection.
But the Redirection Work only if the Header hasn’t already be Sent, so the function has to be executed before production of any other Output.

Step-by-step – Eclipse PHP Fedora 37 Installation

So to Redirect somewhere from inside your Scripts:

  1. The header function need to Result Placed at the Top of the Page or Before any HTML Tag/Content

  2. Execute the header function with an Argument Starting by “Location:”

    header('Location: http://redirect/To/This/URL')

When Not Sure if Headers are already been Sent you need to Couple it with an header_sent() Check like this:

if (!header_sent()) {
header('Location: http://redirect/To/This/URL');
} else {
echo "Cannot redirect, for now please click this
"href="http://redirect/To/This/URL"" .
">link
instead";
}

Tags:

QuickChic Theme • Powered by WordPress