Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP FAQs
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Closed Thread
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 3rd, 2004, 06: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 5 Times in 5 Posts
Default Q. How do I redirect users? (Location Header)

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

Code:
<?php
    header('Location: http://www.thesunmagazine.org');
?>
http://www.php.net/header

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:
Code:
<?php
    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.

Code:
<?php

    ob_start();

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

    ob_end_flush();

?>
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:
http://www.php.net/outcontrol

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.
Closed Thread


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 07: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 06:33 AM
Header functions is same to asp respose.redirect() knight Beginning PHP 3 March 12th, 2004 03:06 PM
header redirect dazednconfused Beginning PHP 5 September 9th, 2003 01:25 PM



All times are GMT -4. The time now is 05:24 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.