Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Redirect not working

Posted on 2012-03-21
12
Medium Priority
?
388 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

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 84

Accepted Solution

by:
Dave Baldwin earned 800 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 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 84

Expert Comment

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

Assisted Solution

by:jrm213jrm213
jrm213jrm213 earned 400 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 111

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

879 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