Solved

PHP Redirect not working

Posted on 2012-03-21
12
380 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
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

630 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