Re-directing to another page, after content has been displayed?

Hey guys,

Just a quick question - I’ve made a site using lots of includes and different files, in one of them (after the headings etc have been displayed) I want to run some PHP/SQL if a certain critiera is met, and re-direct to another page.

Header(“Location: whatever.php”); won’t work unfortunately because there’s already some stuff being output on the page :frowning:

Cheers

Why can’t you move this code to the top of the file that does all the including?

Hmm, I guess I could, but I like to keep everything on each page you know? If I can anyway.

So for example the page that has the form information, I’d like the PHP processing for that form to be at the top of the same .php file - probably not the best way of doing it, but it helps me keep things organised

Then whatever page is including all the other pages should use output buffering so headers in included files get sent first.

http://php.net/manual/en/function.ob-start.php

Cheers Dan, looks a good way of doing it, but I fear I’m too far into this particular site to go back and do it now, will bear it in mind for future projects though! Think I’m going to have to use a javascript re-direct unfortunately.

On a side note I develop systems at work (all just on an intranet) and always use header location in the same way as I described above, and it works fine… is it a server setting, or are there some other shenanigans going on?

Thanks again

Maybe output buffering is enabled at the server level (php.ini) on that system?

The fact that you can’t send headers after output is a logical consequence of these being HTTP responses. In the HTTP protocol, how browsers and servers communicate, the headers are the lines of text before the first blank line in the output, and the body is everything after it. Once you send any output in your PHP code, the server has to send that blank line for it to be interpreted as part of the document body by the browser. Once you’re there, you can’t “take it back” to add in a header… if PHP didn’t throw the warning and sent the header, you’d literally see “Location: http://someurl” as text on the webpage.

Good shout cheers Dan, the developer here before me set up the server and he sets his pages out the same way I do, so it might make sense that he’s turned output buffering on, but of course with this online host I won’t have the choice to do something like that, and it’s probably not the ‘right’ way of doing it either! Will just display a message on the page for now and see how it goes.

I’d say that code structure just must be logical and sensible.
But if code does unnecessary output - it is not sensible. So, the structure must be rewritten. Not because this silly error, but because it must be logical.

Well, basically it’s a page I created for a CMS - they edit the content, and then I’d like for it to re-direct to the CMS’s main listing page with a little message at the top, but I can’t do that unfortunately so I’ll just have to display a “Thank you, the content has been updated” message ;s

edit: OR I COULD JUST INCLUDE THAT PAGE! AWESOMES!

Two rules that must be followed unconditionally:

  1. Any database altering must be dщne with a form using POST method
  2. After processing any POST form, redirect is obligatory.

So, include is not an option.

Where abouts did you come to learn those rules? If it’s not from personal hard-won experience, then a good source may be a useful resource for others to learn from.

  1. Bots dont press buttons. But follow links.
    If you don’t want some erraneous bot deleted your data - any changes must be POSTed
  2. Press F5 with no redirect-after-post. See?

What I want to get from you are recommended resources that people can go to, so that they can learn these good-practice techniques.

Do any exist that meet your standards?

I know no resources.

I agree about the F5 thing, but don’t really see around it unfortunately, other than completely re-cording the site :confused:

With regard to the rule about using POST for ALL updates see the standards - http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

I agree that it’s a good best-practice to abide by. My curiosity was piked as to whether any existing resources were considered to overall meet the same level of high standards.