Greytree

TamWiki

For a mouse who is a packrat

Technology » Handling Redirects In PHP
how to handle the building and execution of redirects in PHP

Summary:this is what goes at the top of the site

(redirected from Main.HandlingRedirectsInPHP)

Redirects are when one script finishes up processing and then calls another. Typically this is handled via the HTTP header Location:. In PHP, headers can be set via the header() function:

  1. header("Location: index.php");

HTTP headers must be emitted before any other output. Thus, if you're code sends error messages to the browser before it gets to the header("Location: ..."); call, that redirect will not work.

Perpetuating certain states with query string additions

Sometimes, it is preferrable to pass on query string options that the script uses to the redirect target script/URL. Frequently, I use this mechanism to turn on debugging (See How To Set Up For Debugging In PHP.) To do this, I use an associative array containing the query string parameters and values:

  1. $additional_query_parms['debug'] = DEBUG;

Then, before emitting output or calling a redirect, add in any acculated error messages you man want to display to the user:

  1. $additional_query_parms['errors'] = $errors;

And build your redirect. I have the following function to do that:

  1. /**
  2.  * Build a redirect path including messages, errors, and additional query data
  3.  *
  4.  * @return redirect url
  5.  * @author Tamara Temple <tamara@tamaratemple.com>
  6.  **/
  7. function buildredirect($u=NULL)
  8. {
  9.     global $errors, $messages, $additional_query_parms;
  10.  
  11.     $redirect = (isset($u) ? $u : DEFAULT_REDIRECT);
  12.     $options = Array();
  13.     if (isset($additional_query_parms) && !empty($additional_query_parms)) {
  14.         $options = $additional_query_parms;
  15.     }
  16.     if (isset($messages) && !empty($messages)) {
  17.         $options['messages'] = $messages;
  18.     }
  19.     if (isset($errors) && !empty($errors)) {
  20.         $options['errors'] = $errors;
  21.     }
  22.     debug_var("\$options:",$options);
  23.     if (!empty($options)) {
  24.         $redirect .= "?" . http_build_query($options);
  25.     }
  26.     return $redirect;
  27.  
  28. }

It makes use of the http_build_query() function, which does a nice job of handling necessary serializing and urlencoding.

The use of the array $additional_query_parms is nice because you can accumulate the parameters throughout your script, and then in a single call build the query string and redirect url.

Debugging and Redirects

Elsewhere I've covered debugging in PHP. Just to reiterate a point made there, often times you don't want your redirect to happen because you will lose debugging information, or the redirect call won't go through because you've already output information to the browser. I have a little do_redirect() function that handles this case:

  1. /**
  2.  * perform a redirect to the indicated url $u, applying other paramters as needed.
  3.  *
  4.  * @return none - will either redirect or exit
  5.  * @author Tamara Temple
  6.  **/
  7. function do_redirect($u=NULL)
  8. {
  9.     if (!isset($u)) $u = DEFAULT_REDIRECT;
  10.     $u = buildredirect($u);
  11.     debug("Redirect: \$u=$u");
  12.     if (!DEBUG) header("Location: $u"); else exit("<p><a href='$u'>Redirect to $u</a></p>");
  13. }

This nicely handles the case where you may have emitted debugging information to the browser already (in which case, the header("Location:...") function won't work) by offering up a link on exit that will in effect do the same thing as the redirect would have.


Tags: Categories: Articles

Recent Changes | Printable View | Page History | Edit Page
Page last modified on April 17, 2012, at 08:59 PM by ImportText?