NYCPHP Meetup

[nycphp-talk] Error checking a simple link

csnyder chsnyder at gmail.com
Sat Nov 22 10:29:43 EST 2008


On Fri, Nov 21, 2008 at 6:21 PM, Tim Lieberman <tim_lists at o2group.com> wrote:
> header("Location: /some/url/or/other");
> exit();
>
> There are slightly more proper ways to write that, but that's the jist of
> it.
>
> You'll need to make sure that there's no output before the header line, or
> turn on output buffering to ensure there isn't.
>

A good pattern for this is

if ( !headers_sent() ) {
  header( "Location: xyz" );
  exit();
}
else {
  exit( "<p>Please continue to <a href='xyz'>xyz</a></p>" );
}

That way you cover the case where there was an error or warning or
something that would prevent the automatic redirect. Of course, you
shouldn't be outputting errors on a production site, etc.

Chris Snyder
http://chxor.chxo.com/



More information about the talk mailing list