edoceo: Latin "to inform fully, instruct thoroughly"

PHP Redirect Function

This is our PHP redirect() function, it has been tested with PHP versions >= 4.2.0. Seen a lot of requests for this on various mailing lists so we thought we'd share.

We've added support for different HTTP/1.1 3XX status codes. Each of the response codes tell the requesting User-Agent how to respond as described below.

In the W3C specification it's stated that the server should also give some content back for the User-Agent to render if it does not obey the redirect. This redirect function will output some pretty HTML if the headers have already been sent following the W3C intentions.

  • 301 Moved Permanently - Forget the old page existed
  • 302 Found - Use the same method (GET/POST) to request the specified page.
  • 303 See Other - Use GET to request the specified page. Use this to redirct after a POST.
  • 307 Temporary Redirect - Good for response to POST.

Usage example

<?php
// Do stuff here before sending headers
redirect('/')
redirect("/my/page")
redirect("http://www.foo.bar/page.htm",303)
redirect("./index.php",307)
?>

The redirect function itself

You may copy and paste this code freely where you need to, please give us credit it if you use it


See Also

© 1999-2008 Edoceo, Inc. Mostly valid XHTML 1.1. and CSS3