Solved

PHP Redirect not working

Posted on 2012-03-21
12
369 Views
Last Modified: 2012-03-26
Hi
I had this code which worked one website.
header('Location: tm_wufoo.php');

Open in new window


So it redirected to the tm_wufoo page once certain conditions were met.
It doesn't work on the new site though. Instead I get the message:
Warning: Cannot modify header information - headers already sent by (output started at blah.org\tm_header.php:25) in blah.org\tm_login.php on line 31

What must I do to get rid of this warning?
Thanks
0
Comment
Question by:jdhackett
  • 3
  • 3
  • 2
  • +3
12 Comments
 
LVL 15

Expert Comment

by:StingRaY
Comment Utility
Try inserting "exit();" after redirect like this:

header('Location: tm_wufoo.php');
exit();

Open in new window

0
 
LVL 4

Expert Comment

by:Paul-B
Comment Utility
You could get around this using javascript.... like

<?php
function MyJavaScriptRedirection($url)
{?>
<script type=”text/javascript”>
<!–
window.location = “<?=$url?>”
//–>
</script>
<?}
print “do stuff”;
MyJavaScriptRedirection(“tm_wufoo.php”);

?>

Open in new window


A better way is to put it all in one tag of possible though.
0
 
LVL 1

Author Comment

by:jdhackett
Comment Utility
Stringray - added exit() but it made no difference. Are there PHP admin settings that could account for the difference in the way the code works?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Any redirect with header('Location: tm_wufoo.php'); must come BEFORE any output to the browser.  Even a space ' ' will cause the other headers to be sent and block sending anymore.  There are no settings which can change that, it's part of the HTTP protocol.
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 200 total points
Comment Utility
0
 
LVL 4

Expert Comment

by:Paul-B
Comment Utility
you might try changing the line to

header('Refresh: 1; url=http://yoursite.com/PATHtoFILE/tm_wufoo.php');

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
Comment Utility
DaveBaldwin is correct.  If you add this line as the first line of your script, PHP will buffer the browser output until the end of the script.  It should get you past the issue of accidentally sending browser output before your script has completed the headers.

ob_start();
0
 
LVL 1

Author Comment

by:jdhackett
Comment Utility
Thanks everyone. Ob_start solved the issue.
Is it considered "bad practice" that I had to do this?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
No, that's what it's there for.
0
 
LVL 17

Assisted Solution

by:jrm213jrm213
jrm213jrm213 earned 100 total points
Comment Utility
I wouldn't say it is considered bad practice, but I also don't think the intended purpose of output buffering was so that people could redirect at any point when they had already started sending output.

The issue you are facing is that the headers have already been sent because of output that was started at line 25 of your tm_header.php file, so you can't change them, which is what your script is trying to do.

I think the best solution (if possible) would be for you to check for the redirect condition in your tm_header.php file before you start sending output.

Output buffering is going to write your output to a buffer in memory, so it is going to increase the memory usage on your server. If your pages are large you can exceed your script limit, or just cause a major slow down on your server (I have no idea what kind of server you are on, just wanted to mention it).
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I generally agree with jrm213jrm213 about the logic that would begin producing browser output before it had completed all of the logic needed to build the page.  Life works better, your scripts are more maintainable that way, etc.

But in my experience the observed behavior of using ob_start is that it makes your scripts run faster.  Instead of connecting and sending several chunks of browser output, it saves all the chunks together in one big chunk until the script terminates, and then sends it all at once.  There is considerable overhead associated with sending each chunk of browser output, thus reducing the number of chunks can reduce the total overhead.
0
 
LVL 1

Author Comment

by:jdhackett
Comment Utility
Thanks everyone for their help, very educational.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now