Topic: ob_start XHTML->HTML not working

On my forum, I serve my pages as XHTML with application/xhtml+xml to any browser that supports it based on the accept-type header.  For browsers that don't, I use an ob_start with a converter function to convert it to HTML, and send it as text/html.  I have tested the converter script with WordPress, so I know it works.  I use the include tag in the main template file to include the PHP script which converts the output.  Unfortunately, I am running into problems.  The MIME type is set correctly, and the DOCTYPE and <html> tag that it directly outputs are correct.  But everything that the ob_start is supposed to do never happens; the output is still XHTML (not HTML) when I use a browser that does not support XHTML.  This problem only occurs with PunBB; the exact same script works fine when added to a WordPress template.

Anyone have any ideas?  Is it necessary for me to post my converter script?

Thanks in advance.

Re: ob_start XHTML->HTML not working

Could the fact that PunBB uses ob_start in certain situations have something to do with it?

Re: ob_start XHTML->HTML not working

Smartys wrote:

Could the fact that PunBB uses ob_start in certain situations have something to do with it?

Yeah, that could be it.  But the PHP documentation says that ob_start can be nested with no bad effects.  So if that is the problem, it's probably a bug in PunBB.  What kind of usage in PunBB would screw up an ob_start that has already been called?  And if that is the problem, how would I fix it?  Would it be as simple as adding an ob_end_flush somewhere?  If so, where would I add it?  Could the problem be caused by the fact that my ob_start doesn't have an ob_end_flush associated with it (it just ends at the end of the file)?

Sorry for being such a n00b and asking so many questions.  Thanks in advance.

4

Re: ob_start XHTML->HTML not working

Why would you convert everything to HTML? It is ok to send XHTML 1.0 as text. XHTML 1.1 is the one that should be send as application/xhtml+xml only...

http://www.info-mob.com/forum/ - Croatian forum only, don't bother if you don't speak Croatian :)

Re: ob_start XHTML->HTML not working

gog wrote:

Why would you convert everything to HTML? It is ok to send XHTML 1.0 as text. XHTML 1.1 is the one that should be send as application/xhtml+xml only...

a) I am using XHTML 1.1.
b) Even XHTML 1.0 can only be sent as text/html if it is in compatibility mode, which I do not use.
c) There is no point in using XHTML anyway if it can be sent with an HTML MIME type and still be read as intended, since then it is basically HTML with weird slashes and various validation errors.
d) I am considering using XForms with an XML Schema for XHTML 1.1 + XForms 1.0.  Since there is no DOCTYPE on that version, if I sent it to a browser as the HTML MIME type, IE would likely go into Quirks Mode, which is not what I want to happen.
d) That wasn't relevant to my question anyway; this could just as easily be about an ob_start that converted it to l33t sp33k, added white space to indent stuff, or anything else that an ob_start could do.

Anyone have an answer to my original question?

Thanks in advance.

6

Re: ob_start XHTML->HTML not working

offtopic:

What do you get out of serving application/xhtml+xml?

Face it, IE is here, and it is going to be here for a long time sad

http://www.info-mob.com/forum/ - Croatian forum only, don't bother if you don't speak Croatian :)

7

Re: ob_start XHTML->HTML not working

My reading of the php manual is that where there is nesting then for each ob_start there must be an equal number of ob_end_clean or ob_end_flush. PunBB always uses ob_end_clean.

Output buffers are stackable, that is, you may call ob_start() while another ob_start() is active. Just make sure that you call ob_end_flush() the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order.

Re: ob_start XHTML->HTML not working

Paul wrote:

My reading of the php manual is that where there is nesting then for each ob_start there must be an equal number of ob_end_clean or ob_end_flush. PunBB always uses ob_end_clean.

Output buffers are stackable, that is, you may call ob_start() while another ob_start() is active. Just make sure that you call ob_end_flush() the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order.

Thanks for the reply.  Unfortunately, I'm not familiar enough with the coding of PunBB to know how to fix this.  Adding an ob_end_flush at the end of my template doesn't seem to fix it.  Any ideas on what PunBB files I should edit that would fix this?

Thanks in advance.

9

Re: ob_start XHTML->HTML not working

Thinking about it, the problem is probably quite obvious. The problem is that <pun_include> is output last of all. By that time PunBB has already merged its output with the template and flushed the buffers. That would leave your function with nothing to work with. It seems to me the only way to get this to work is to add your function as the last item in footer.php and then run your function against $tpl_main (which is holding all the merged content).

Re: ob_start XHTML->HTML not working

Paul wrote:

Thinking about it, the problem is probably quite obvious. The problem is that <pun_include> is output last of all. By that time PunBB has already merged its output with the template and flushed the buffers. That would leave your function with nothing to work with. It seems to me the only way to get this to work is to add your function as the last item in footer.php and then run your function against $tpl_main (which is holding all the merged content).

Just tried your suggestion, and it worked like a charm!  Thank you so much for dealing with my n00b questions in such a helpful way.