Last updated 1 October 2011. Created on 29 October 2009.
Edited by JuliaKM, figaro, pawel.traczynski. Log in to edit this page.

If you have a block that you want to set visible only for logged in users, follow these easy steps:

  1. Go to Administration -> Build -> Blocks page;
  2. Select "Configure" link next to the name of the block that you want to set visible only for logged in users;
  3. After the page reloads scroll down to the "Page specific visibility settings" field, select "Show if the following PHP code returns TRUE (PHP-mode, experts only)" option and paste the following code in the field "Pages" showed below the selection options:
  global $user;
  return (bool) $user->uid;

To display the block only for anonymous users use the following code:

  global $user;
  return !(bool) $user->uid;

Note: to use this method the PHP filter module must be enabled!

Looking for support? Visit the forums, or join #drupal-support in IRC.


amandalaine’s picture

The "Show if the following PHP code returns TRUE (PHP-mode, experts only)" field exists in Drupal7, so I tried these steps anyway (even though above it says it's only for Drupal6), and it doesn't work.

Anyone know how to fix that? This seems like such a handy fix... but it doesn't work in Drupal7.

amandalaine’s picture

I found out this does work ONLY if you apply it to a block you've created. If you apply it to a default block, it gets ignored.

However, it's a default block that I NEED to change. (I want to set the content region to only be visible for authenticated users.) I also made other changes to default blocks and not ONE of those took. Only changes that I make to a block, which I've created, actually take effect.

That's a problem. Why?

gerbreown’s picture

If you go to:
Visibility settings
Show block for selected role, you can select the role that you want to see/not see the block.

This works for all blocks.

Hope this helps.

armin1980’s picture

I created a block I want only anonymous user see. I used the above php code however when I use the block as a content in the panel, it simply ignores the PHP code. what should I do?

dhineshr’s picture

Thanks it helped me!!

see15_aug’s picture

Thanks. It helps me

mesaonka’s picture

Yeah. It is also help me.

My website

Summit’s picture

Within panels the phpcode is ignored as told earlier in this thread.
Within Panels there is no phpcode necessary for this..within visibility settings you can choose which users see the pane.
greetings, Martijn