PLEASE NOTE! The following snippets are user submitted. Use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

/**
* The following simple snippet
* displays different information to anonymous/logged in users within a page.
*
* This works with drupal 4.5 and drupal 4.6
*/
global $user;
if ($user->uid) {
    return "This message is only visible for logged-in users.";
} 
if (!$user->uid) {
    return "This message is only visible for not-logged-in users.";
} 

Comments

Anonymous’s picture

Simply create a page, that would only have the title and URL, and no content, then create two blocks : one for the authenticated users, one for non-authenticated users (you can control this by checking who can see the block when creating it). Assign both block to the content area, only on that specific page. Now users will see the block in the content, and only the block that the user is suppose to see.

This technique is more flexible, as you can even specify as many roles and different content as you want, it is also entirely editable through the drupal interface... and it doesn't need any php

______________________________

www.raincitystudios.com

benlotter’s picture

As a newbie to PHP and Drupal, it's refreshing to see someone proposing solutions without the "knee-jerk" response of doing this in PHP. Great solution!

EDIT
I was able to adapt the above solution to display customized Access Denied pages based on whether the user was logged in. Here are the steps I used for my Drupal 6.1x site.

  1. Create a node of content type Page and name it Access-Denied but leave the body blank.
  2. Go to Admin > Site Configuration > Error Reporting and enter your custom path in the 403 (access denied) line. Enter "Access-Denied".
  3. Create a 2 block, configure them and add them to the content area. (I named mine "Access Denied: Contact Admin" and "Access Denied: Please Login".)
  • Access Denied: Contact Admin settings:
    Body: This page cannot be displayed due to insufficient privileges. If you believe you should have access to it, please contact your site administrator.
    Show Block for: Authenticated Users
    Show Block on Specific Pages: Show on only the listed pages, Access-Denied
  • Access Denied: Please Login settings:
    Body: The content you are trying to view requires you to be logged in. Please Sign In to continue.
    Show Block for: Anonymous Users
    Show Block on Specific Pages: Show on only the listed pages, Access-Denied