Redirecting the user in PHP

PHP/MySQL
Last Updated: 2007-01-23 20:54:27
STEP 1: Using Header()
When a script communicates with a client browser, the server will send headers to the client to let the browser know how to handle the server document.

To send headers to the client manually we use the header() function. Before you use the header() function, you must make sure that no output has been sent to the client browser first. There is a way around this using output buffering that I will go over later.

When relocating a user we send the "Location" header to the client. The syntax looks like this:
header("Location: yourPage.php");
or like this:
header("Location: http://www.joelucky39.com");

STEP 2: Output Buffering
If you must put your redirect at the bottom of a script following some client output then we must use output buffering. For instance if your script requires an include file that occurs before you can place your header() function, the server will send output to the client prior to your redirect.

Output buffering insures that the script is totally run and completed prior to any headers being sent to the client browser.

You begin output buffering using the ob_start(); function and close it with the ob_end_flush(); function.

So the syntax would look like so:

<?
ob_start();

Your script here


header("Location: http://www.joelucky39.com");

ob_end_flush();
?>

Back to Tutorial List

This is the JoeLucky39 tutorial section. Feel free to use whatever information you find here on your own site(s). Know that not all tutorials are ideal for all sites, so feel free to modify the information contained in this tutorail section as best suits your web site.

At JoeLucky39 you can find tutorials on Comic Art, PHP/MySQL, and more are added all the time. So, visit often and feel free to contact Joe if you think that his tutorials need to be changed or updated.

Joe believes in the open source movement and is happy to share his knowledge with anyone who asks. All Joe asks is, if you use Joe's open source tutorials, you share your knowledge as well.
Thank you and I hope you find what you are looking for. If not feel free to contact me and ask.
LOG IN:
© Copywrite 2010 JoeLucky39.com