Jump to content

Recommended Posts

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.

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

... 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.

 

  • 2 weeks later...

 

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.

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 by Christian F.

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

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. ;)

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!

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.