Jump to content

Alternative to header redirect


alena1347

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

  • 2 weeks later...

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.

Archived

This topic is now archived and is closed to further replies.

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