PHP Redirecting With the Header Function and Location

October 21, 2011 | By Duchateaux.

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.

PHP Redirecting with the header function and Location - Featured

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 {
/necho "Cannot redirect, for now please click this
"href="http://redirect/To/This/URL"" .
">link
instead";
}


QuickChic Theme • Powered by WordPress