Drush status: unrecoverable error caused by drupal_goto() in hook_init()
I have a module with a drupal_goto() that redirects users if they don't pass the age requirements (wine site) in hook_init(). The module is set with a heavier weight so it loads last.
On PHP 5.3, drush update fails. The same code and DB on a different server with PHP 5.2 runs fine.
Drush version: 7.x-4.4 PHP version: 5开发者_C百科.3 example:
When I run: drush status, I get an error. Here is the output with the debug and verbose flag.
$ drush -d -v status
Bootstrap to phase 0. [0.02 sec, 2.47 MB] [bootstrap]
Drush bootstrap phase : _drush_bootstrap_drush() [0.03 sec, 2.67 MB] [bootstrap]
Drush bootstrap phase : _drush_bootstrap_drupal_root() [0.06 sec, 5.49 MB] [bootstrap]
Initialized Drupal 6.22 root directory at /var/www/example.com/public_html [0.07 sec, 6.28 MB] [notice]
Drush bootstrap phase : _drush_bootstrap_drupal_site() [0.07 sec, 6.29 MB] [bootstrap]
Initialized Drupal site default at sites/default [0.07 sec, 6.29 MB] [notice]
Drush bootstrap phase : _drush_bootstrap_drupal_configuration() [0.08 sec, 6.29 MB] [bootstrap]
Drush bootstrap phase : _drush_bootstrap_drupal_database() [0.08 sec, 6.33 MB] [bootstrap]
Successfully connected to the Drupal database. [0.08 sec, 6.33 MB] [bootstrap]
Drush bootstrap phase : _drush_bootstrap_drupal_full() [0.09 sec, 6.67 MB] [bootstrap]
Drush command terminated abnormally due to an unrecoverable error. [0.34 sec, 31.38 MB] [error]
The question is now: Why does the drupal_goto() cause drush to fail on the PHP 5.3 server and not the PHP 5.2 server.
You should not issue redirects when there is an access denied. That is a logical error in any web-framework. You should issue an "access denied" when you want to deny access. Just like you issue a "the content has moved there, please go there" when your content has moved.
Instead of drupal_goto()
, use drupal_access_denied()
. If you truly want people to be redirected to another url, the place to do that, would be on the page that shows the denied access.
That said, issuing a drupal_access_denied()
, or, in fact, sending any header in hook_init()
is plain wrong. From the documentation: «Perform setup tasks.» Sending headers is not a setup-task. Moreover, that same documentation warns about another potential problem with your approach: «This hook is not run on cached pages», in other words, once an adult visits a page, and it is cached, underaged visitors will not be redirected or denied access, they will get the cached page served.
Now, on to what you should do: A module such as Taxonomy Access (Lite) or the more appropriate Node Privacy By Role can be used to grant access to your content. Note that, by default, Views will not use these access: they will show lists of content, regardless of the access by the user: you will have to reconfigure all your views to either honor the access-rules or to be inaccassible entirely, to certain roles. In addition, you need to give "minors" a separate role: that way you can grant them different access then the people in rol "authenticated user" or even in "adult". You will need some mechanism to automatically assign roles.
精彩评论