Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP FAQs
| Search | Today's Posts | Mark Forums Read
PHP FAQs This moderated forum is where our PHP experts answer some of the questions they see asked most frequently in the other PHP forums. You cannot post other messages here, use the other PHP forums for that.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP FAQs section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 3rd, 2004, 07:40 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default Q. How do I redirect users? (Location Header)

In PHP you can redirect a user to another page using the header() function.

    header('Location: http://www.thesunmagazine.org');

This function is used to modify the HTTP response headers sent out from the server. For this example the 'Location' header is modified, the browser then picks up on the modification and makes a new request from the new location specified.

Because this function modifies the HTTP headers, it may not be included after output has started from the script.

For example:
    echo 'Hello, world!';
    header('Location: http://www.thesunmagazine.org');
This throws a warning-level error saying something to the effect of headers cannot be modified, output already started at line x in file z.

This, on the other hand, is valid because it uses output control functions to buffer script output.



    echo 'Hello, world!';
    header('Location: http://www.thesunmagazine.org');


Output from the script is written to the buffer, therefore, when the script reaches header() it is able to make HTTP header changes since nothing has yet been sent to the browser.

Output buffering increases resource consumption slightly because extra memory has to be allocated for the buffer.

More on output buffering:

Also, as a side note, it doesn't make any sense to have need of outputting anything before redirecting a user with the 'Location' header since all output will be lost anyway. However, this does come in handy for other functions or header modifications such as session_start() or setcookie() both of which make modifications to the outgoing HTTP headers. If you find you're in need of showing the user output before redirection, this is better done on the client-side with JavaScript.

Similar Threads
Thread Thread Starter Forum Replies Last Post
header("location:./in.php"); aneesh_sunu@yahoo.co.in PHP How-To 0 November 25th, 2006 08:27 AM
header redirect not working guillermo Beginning PHP 2 June 23rd, 2006 05:33 AM
header location+querystring Ashleek007 Beginning PHP 5 February 13th, 2006 07:33 AM
Header functions is same to asp respose.redirect() knight Beginning PHP 3 March 12th, 2004 04:06 PM
header redirect dazednconfused Beginning PHP 5 September 9th, 2003 01:25 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.