Solved

PHP Redirect not working

Posted on 2012-03-21
12
374 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
ID: 37748354
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
ID: 37748404
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
ID: 37748633
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37748843
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 83

Accepted Solution

by:
Dave Baldwin earned 200 total points
ID: 37748851
0
 
LVL 4

Expert Comment

by:Paul-B
ID: 37749174
you might try changing the line to

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

Open in new window

0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 37750039
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
ID: 37758022
Thanks everyone. Ob_start solved the issue.
Is it considered "bad practice" that I had to do this?
0
 
LVL 83

Expert Comment

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

Assisted Solution

by:jrm213jrm213
jrm213jrm213 earned 100 total points
ID: 37758518
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 109

Expert Comment

by:Ray Paseur
ID: 37759178
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
ID: 37765301
Thanks everyone for their help, very educational.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

803 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