i was looking for a way to show a block for anonymous users only... so i found this code from the hand books and tried it...

//

global $user;
if (!$user->uid) : 

This content will be visible only to anonymous users.

 else {
    return;
}
endif;

//

but still this is not working and showing an error..

do you know a better way? another way to make a block that shows only to the anonymous users...

Comments

Dublin Drupaller’s picture

your code doesn't look right famo...try ALL these steps:

  1. click on ADMINISTER -> BLOCKS
  2. click on ADD BLOCK
  3. in the "block body" text area paste in the snippet displayed below
  4. Click on the PHP CODE filter option
  5. Save your block
  6. enable your block
  7. Log out
<?php 
global $user; 
if (!$user->uid) 
{ print "the stuff you want to display to anonymous users only";}
?>

hope that helps

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

famo’s picture

the code is working now.. and i can use this block to post lists and write text but i cannot add a link...

i want it to be able to show a link but it's not working.
check how i've changed

///

global $user;
if (!$user->uid)
{ print "<a href="/www.site.com">my link</a>";}

///

it's not working and so i got this error

Parse error: parse error in c:\web\site\includes\common.inc(1813) : eval()'d code on line 4

famo’s picture

the code is working now.. and i can use this block to post lists and write text but i cannot add a link...

i want it to be able to show a link but it's not working.
check how i've changed

///

global $user;
if (!$user->uid)
{ print "<a href="/www.site.com">my link</a>";}

///

it's not working and so i got this error

Parse error: parse error in c:\web\site\includes\common.inc(1813) : eval()'d code on line 4

Dublin Drupaller’s picture

note that you have to comment out the quotes \".

<?php
global $user;
if (!$user->uid)
{ print "<a href=\"www.site.com\">my link</a>";}
?> 

hope that helps..

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

famo’s picture

that helps... exactely what i want..

thanks dub :)

famo’s picture

i don't understand why the block name appears when the user loged in?

the content of the block appears to the anonymous users only, that's good, but when the user log in the name of the block appears and i dont want its name to appear...

can you tell me how to hide the name of the block when the user logs in?

famo’s picture

do you know how to help me in this : not to let the block name appear even when its content is not visible to the loged in user?