alena1347 Posted February 5, 2013 Share Posted February 5, 2013 I have a php file and need to redirect to other pages from middle of the page as header() needs to be put on the top is there any other way please. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 5, 2013 Share Posted February 5, 2013 There are hacks that work around this, but they are of very varying quality and thus not recommended to use. What you really should do is to restructure your site. Put all of the PHP code that processes data at the top of the page, and don't send out anything to the client until you've parsed all of the PHP code. Doing it this was removes a lot of artificial limits that you've imposed on the code, by mixing HTML and PHP, and it will make your life a lot easier in the future. Quote Link to comment Share on other sites More sharing options...
cutee4 Posted February 12, 2013 Share Posted February 12, 2013 There may be any bug or error in site. According to me, make changes in code or change the site pages.. Or reinstruct. Quote Link to comment Share on other sites More sharing options...
alena1347 Posted February 12, 2013 Author Share Posted February 12, 2013 There may be any bug or error in site. According to me, make changes in code or change the site pages.. Or reinstruct. It's not that, the header must be put before any output and I need to redirect after some output, I am currently using javascript window.location but need something else in php Quote Link to comment Share on other sites More sharing options...
kicken Posted February 12, 2013 Share Posted February 12, 2013 ... but need something else in php There is nothing else. header() is how you do a redirect from within PHP. Your options are either re-structure your code so you can use header() or continue using a workaround like JS. Quote Link to comment Share on other sites More sharing options...
spencerharry80 Posted February 14, 2013 Share Posted February 14, 2013 I followed the procedure stated in the earlier reply, but now it is giving me a scripting error. Quote Link to comment Share on other sites More sharing options...
alena1347 Posted February 18, 2013 Author Share Posted February 18, 2013 I followed the procedure stated in the earlier reply, but now it is giving me a scripting error. could you post the code a little bit so we can solve it Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted February 27, 2013 Share Posted February 27, 2013 You could use a meta refresh, can also place a die() or exit() after it <meta http-equiv="refresh" content="0;URL='http://mysite.com/'"> Quote Link to comment Share on other sites More sharing options...
trq Posted February 27, 2013 Share Posted February 27, 2013 It's not that, the header must be put before any output and I need to redirect after some output, That makes no sense. Why are you outputting anything if all you are going to do is try and redirect the user to another page? You need to fix your logic. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 27, 2013 Share Posted February 27, 2013 (edited) It should be noted, that you can (and probably should) sent the refresh header via the header () function in PHP. As hinted to by the meta-tag's name http-equiv: header ("Refresh: 5; URL=http://mysite.com/"); Edited February 27, 2013 by Christian F. Quote Link to comment Share on other sites More sharing options...
alena1347 Posted March 4, 2013 Author Share Posted March 4, 2013 That makes no sense. Why are you outputting anything if all you are going to do is try and redirect the user to another page? You need to fix your logic. There are situations when you need to put the header after some output code in a page or within an if condition after some output or multiple headers in a piece of code for which this is required Quote Link to comment Share on other sites More sharing options...
Christian F. Posted March 4, 2013 Share Posted March 4, 2013 No, there are no such situations. At least not if you've written the code properly, with separated business logic and presentation logic. The trick is that you do not send anything to the screen/browser, until everything has been processed by PHP. At which point any headers that needs to be sent has been sent a long time ago, and you know exactly what needs to be sent to the browser. Templating engines utilize this logic separation, and as such it might be a good idea to study up on them. Should help you write even better code, even if you choose not to use one for a project. Quote Link to comment Share on other sites More sharing options...
alena1347 Posted March 4, 2013 Author Share Posted March 4, 2013 No, there are no such situations. At least not if you've written the code properly, with separated business logic and presentation logic. The trick is that you do not send anything to the screen/browser, until everything has been processed by PHP. At which point any headers that needs to be sent has been sent a long time ago, and you know exactly what needs to be sent to the browser. Templating engines utilize this logic separation, and as such it might be a good idea to study up on them. Should help you write even better code, even if you choose not to use one for a project. OK got that thank you! Quote Link to comment Share on other sites More sharing options...
Christian F. Posted March 4, 2013 Share Posted March 4, 2013 You're welcome. Quote Link to comment Share on other sites More sharing options...
KirkLogan Posted March 5, 2013 Share Posted March 5, 2013 It may also be worth noting (and correct me if im wrong here) You can enable output buffering, at which point PHP Header functions will not be limited to Pre-Content execution. Christian is right though, it is best practice to keep your redirects prior to page content. Just thought id weigh in. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.