PHP Redirecting with the header function and Location

October 21st, 2011 by Regaz

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 {
echo "Cannot redirect, for now please click this
"href="http://redirect/To/This/URL"" .
">link
instead";
}
(Visited 1 times, 1 visits today)

Tags:


No Responses to “PHP Redirecting with the header function and Location”

  1. Anonymous says:

    why like this on win7?seems to work on xp ?!?!