Solved

PHP Redirect not working

Posted on 2012-03-21
12
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 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 110

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 110

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

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

756 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