Redirect from within block controller - on_start()

Permalink
Hi

I have seen at least one add-on which lets you create a block which redirects a user.

I am writing a custom block and require this functionality, I have attempted to overwrite on_start() but this does't work and results in "Warning: Cannot modify header information"

How can I do this before anything is output to the browser?

Thanks

BinaryFold4
 
olliephillips replied on at Permalink Reply
olliephillips
That's most likely a generic PHP error - something has already been output before you attempt the redirect (which is done in the headers).

These problems can be hard to debug/find. You're looking for any html even whitespace that has already been sent to browser, so headers and hence the redirect can't be set.

If you're not reliant on setting Http status codes (301 etc), you could be lazy and do it with javascript and you could code that as late as your view.php file.
BinaryFold4 replied on at Permalink Reply
BinaryFold4
Yup, I thought this might be the case, it could be anywhere. Gr.

Js redirect no good in this case I am afraid.
jordanlev replied on at Permalink Reply
jordanlev
I don't think you can do this from a block controller -- by the time the block code is called I think the system has already started outputting header html to the browser (and you can't do a redirect once anything has been outputted to the browser -- even just one little space character).

What you can do, though, is make a package, and in the package controller's on_start() method you can do a redirect. This is definitely called before any html is outputted.
BinaryFold4 replied on at Permalink Reply
BinaryFold4
This package, which contains a block seems to be able to do this?

http://www.concrete5.org/marketplace/addons/redirect1/...

Although using this block in place of my block results in the same error which suggests that HTML is being rendered before it's meant to be.

Most of the page is being rendered before my block is called. The above block from the Marketplace I had used successfully before within a site, so you must be able to redirect from within a block?
jordanlev replied on at Permalink Reply
jordanlev
Hmmm... perhaps I am wrong then about not being able to do this from a block.

If this is the case, then the problem is probably due to some extra space or lines in a php file either before the opening <?php or after the final closing ?>

Unfortunately I have no idea how to check for where this is. You'll need to go through your template files, and any customizations you've made to this particular site. If there's even one space before the opening <?php in a file or one space after the closing ?> in a code file, that may be the cause here. But this only applies to files that are primarily code -- not the page type templates or anything that has html interspersed throughout.
BinaryFold4 replied on at Permalink Reply
BinaryFold4
Thanks, I better start digging.