I decided to give Drupal a try last week and downloaded Version 6.0. I've been theming it with some success (pure CSS), however I want to change the "Access Denied" error that is shown when "view node content" is turned off for anonymous users.

Currently, an anonymous user visiting the site sees "Access Denied. You are not authorized to access this page." I've searched through all the files in my Drupal installation and can't figure out where to modify this. I'd also like to be able to change the page title from "Access denied | Example.com" to something else.

Comments

BrianKlinger’s picture

In the Drupal Cookbook (for beginners) which can be found in the tutorials section of drupal.org, you'll find this page: http://drupal.org/node/120646 which discusses custom 403 (Access Denied) pages and 404 (Page Not Found) pages.

jfall’s picture

By providing a custom 403 & 404 page, you have total control, and the use is never disoriented by seeing a page that is "outside" your site - they stay in, even when an error occurs. And it's very easy, and no hacking!

But not this way \/ \/ \/ \/ (hacking core is a sin - did anyone mention that ;-)

jonconnor’s picture

This method doesn't work if you have "view node content" turned off for anonymous users. Please read my original post again carefully.

Custom 403 and 404 pages don't work in Drupal if the user can't see node content, since the error page itself is a node.

jfall’s picture

Hmmmm.
I'd probably write a little custom hook_access module to change the access to these two pages on the fly. I think it should be a fairly easy task... how are your PHP skills?

jfall’s picture

What I did was copy the blog module from core, modified the .info and .module files to create an "error_page" node type, and added a new case to error_page_access to return true when $op == 'view'.

This was pretty easy, as mostly it just involved re-naming, search and replace, and deleting most of the unused parts of the blog module.

I am just learning how to develop in Drupal and PHP, so this was a good exercise for me.

There may be an easier way to hook directly into page_access so you could override $op == 'view' just for those specific nodes, and thus avoid introducing a special data type, but perhaps the new data type keeps the design cleaner and more transparent?

styro’s picture

For nodeless error messages (ie they don't appear in searches etc):

http://drupal.org/project/customerror

Not sure how far away a Drupal 6 version is yet though. It's what we've used since Drupal 4.6.

Or for quickly altering a few translatable strings on Drupal 6 without resorting to using the full locale module:

http://drupal.org/project/stringoverrides

--
Anton
New to Drupal? | Troubleshooting FAQ
Example knowledge base built with Drupal

silverwing’s picture

But not this way \/ \/ \/ \/ (hacking core is a sin - did anyone mention that ;-)

I said twice it wasn't recommended! It could work, but not recommended. Didn't I say it wasn't recommended?

~silverwing - not recommended ;)

_____________________________________________
MisguidedThoughts | showcaseCMS

jfall’s picture

should have read:

But not this way \/ \/ \/ \/ (hacking core is a sin - did anyone mention that this was not recommended? ;-)

:) lost in translation!

silverwing’s picture

This method isn't recommended (and there should be a better - non-hackish way - do to this, but if you open includes/common.inc go to line 390 and change this $return = t('You are not authorized to access this page.');

Remember, this is a core hack and if you upgrade, it'll be lost.

~silverwing

_____________________________________________
MisguidedThoughts | showcaseCMS

matkeane’s picture

If the warning is run through the t() function, you could use the locale module to change the string that is returned. Although it's main function is for multi-lingual sites, the locale function offers a handy way to change certain strings (for example, changing 'Submit' to 'Save' on node forms, so that users know what the button does!)

Matthew

jfall’s picture

Here's another: http://drupal.org/project/r4032login

This module could probably be altered to handle 404 as well?

reaneyk’s picture

I was able to update the 403 text with the following code inside the template.php of my theme:

function phphtemplate_preprocess_page(&$variables) {
  if ($variables['title'] == 'Access denied') {
    $variables['content'] = t('Your updated message here');
  }
}
tevih’s picture

This seems like the most sensible method. Thanks! Works great!

scott859’s picture

Thanks, yes works fine, and I think it's the best solution (not hacking core).

Scott

VikrantR’s picture

I used it and it work for me, Thanks !

function MyTheme_name_preprocess_page(&$variables) {
  if ($variables['title'] == 'Access denied') {
    $variables['content'] = t('Your updated message here');
  }
}

Vikrant R

jphelan’s picture

I get a Notice: Undefined index: title error. So it looks like the title is not set yet. However this works:

function mytheme_preprocess_page(&$vars) {
  $status = drupal_get_http_header("status");  
  if(isset($status) && $status == '403 Forbidden') {
    $vars['title'] = 'Members Only';
    $vars['page']['content']['system_main']['main']['#markup'] = t('Please Login To Access This Page.');
  }
}
Anonymous’s picture

That's the version for Drupal 7. It works even if the anonymus users are not allowed to see any content:

function mytheme_preprocess_page(&$vars) {
  $status = drupal_get_http_header("status");  
  if(isset($status) && $status == '403 Forbidden') {
    $variables['title'] = 'Members Only';
    $variables['page']['content']['system_main']['main']['#markup'] = t('Please Login To Access This Page.');
  }
}
apostl3pol’s picture

I'm just doing a redirect to the login page here, but...

// Redirect to login or home page if access denied
$page_info = menu_get_item();
if ( ! $page_info["access"] ) {
    header( "Location: " . $variables["front_page"] . "user/login/?destination=" );
}
lancewig’s picture

For Drupal 7, this is incredibly simple by using only the GUI.
First create a page and add whatever message you want.
Then customize the page alias as you see fit. Copy that.
Go to admin/config/system/site-information and then go to the field set for "Error Pages" and put in your custom page alias for the 403 page.

jphelan’s picture

Yes, you are correct, this should work for majority of people. I however needed to redirect the user back to the page after they logged in and since setting the error page that way makes it an actually node it ends up redirecting back to the node you are using as your 403 page. Which is obviously confusing to users since they just logged in. Using mytheme_preprocess_page gives me the correct destination parameter on the access denied page.