This is a very simple block to display a link to the login page when the user is not logged in and to display a logout link when the user is logged in. Create a custom block and set the input filter to php.

Drupal 4.6

 global $user;
 if (!$user->uid) {
  // Change the following line's Login/Register text to whatever you want.
  return l("Login/Register", "user/login");
 } else {
  // The following line will display the username you are logged in as.
  return 'Logged in as ' . check_plain($user->name) . '<br><a href="/?q=logout">Logout</a>';
 }

If you don't want the block to redundantly show up in the login section, add this to the custom block's path field.

<^(?!user)>

Drupal 5

This one shows the login form or a link to logout:

global $user;
if ($user->uid) {
print l("Log Out " . $user->name,"logout");
} else {
print drupal_get_form('user_login_block');
}

This shows only links:

global $user;
if ($user->uid) {
print l("Log Out " . $user->name,"logout");
} else {
print l("Log In","user");
}

Comments

spooky69’s picture

If you want to get taken back to the original page you were on when logging in from a text link then replace:

print l("Log In","user");

with:

print l("Log In","user/login" .drupal_get_destination());

Works on Drupal 5.1 (probably 4.x as well?) and with thickbox login module.

zarko’s picture

This variant will return the user to the current page after login/logout.

global $user;
if ($user->uid) {
  $text = "Log Out " . $user->name;
  $path = "logout";
} else {
  $text = "Log In";
  $path = "user";
}
print l($text, $path, array(), drupal_get_destination());

Or, if you prefer a button:

global $user;
if ($user->uid) {
  $img_path = 'path/to/logout.png';
  $text = "Log Out " . $user->name;
  $path = "logout";
} else {
  $img_path = 'path/to/login.png';
  $text = "Log In";
  $path = "user";
}
print l(theme('image',$img_path,$text,$text), $path, array(), drupal_get_destination(), NULL, FALSE, TRUE);
arisaves’s picture

This doesn't appear to be working in Drupal 6. I'd a button [no text] that changes upon user login/logout [as described above.] The code should work... but no dice.

ttosttos’s picture

A good idea to remove visibility using this pattern:
user/login*

istar100’s picture

This is my first community contribution. I am learning to love drupal!

global $user;
if ($user->uid) {
  $edit_account_link = l(t('Your Account'), 'user/$user->uid/edit');
  $logout_link = l(t('Logout'), 'logout');
  $user_utilities = t('Welcome back, ') . $user->name. ' ' . $edit_account_link . t(' | ') . $logout_link;
}
else {
  $login_link = l(t('Login'), 'user', array(), drupal_get_destination());
  $register_link = l(t('Register'), 'user/register', array(), drupal_get_destination());
  $user_utilities = $login_link . t('/') . $register_link;
}
$search_link = l(t('Search'), 'search/node');
return $user_utilities . t(' | ') . $search_link;
udig’s picture

istar100 thanks for the code. Exactly what I was looking for. Just minor correction.

Instead of:

$edit_account_link = l(t('Your Account'), 'user/$user->uid/edit');

it's:

$edit_account_link = l(t('Your Account'), 'user/'.$user->uid.'/edit');
EHA’s picture

I love this snippet from istar100. Here's a 2-line layout of the same, already with udig's correction.

The greeting is on the first line and the login/logout +utilities below. Useful for placing the block on narrower regions. Actually I just added a line break after the welcome greeting.

Works also on Drupal 6. Happy snippeting. ;)

<?php
global $user;
if ($user->uid) {
$edit_account_link = l(t('Your Account'), 'user/'.$user->uid.'/edit');
  $logout_link = l(t('Logout'), 'logout');
  $user_utilities = t('Welcome back, ') . $user->name. '. <br/>' . $edit_account_link . t(' | ') . $logout_link;
}
else {
  $login_link = l(t('Login'), 'user', array(), drupal_get_destination());
  $register_link = l(t('Register'), 'user/register', array(), drupal_get_destination());
  $user_utilities = $login_link . t('/') . $register_link;
}
$search_link = l(t('Search'), 'search/node');
return $user_utilities . t(' | ') . $search_link;
?>
einsicht’s picture

Edit: Thanks a ton for the code and idea, it was very useful!

I don't know about others, but the redirect to destination after login didn't work for me using the above code.
Here's how I did it, so that the alias is shown in the link if available:

global $user;
//added: redirect to previous page
$redirect = 'destination='.drupal_get_path_alias(substr(urldecode(drupal_get_destination()), 12) );
if ($user->uid) {
$edit_account_link = l(t('Your Account'), 'user/'.$user->uid.'/edit');
//changed this
  $logout_link = l(t('Logout'), 'logout', 
               array( 'query' => $redirect, 'alias' => TRUE )
               );
  $user_utilities = t('Welcome back, ') . $user->name. '. <br/>' . $edit_account_link . t(' | ') . $logout_link;
}
else {
//changed this
  $login_link = l(t('Login'), 'user', 
              array( 'query' => $redirect, 'alias' => TRUE )
              );
//changed this
  $register_link = l(t('Register'), 'user/register', 
               array( 'query' => $redirect, 'alias' => TRUE )
               );
  $user_utilities = $login_link . t('/') . $register_link;
}
$search_link = l(t('Search'), 'search/node');
return $user_utilities . t(' | ') . $search_link;

If you don't care about aliases, you can change $redirect to something prettier:

$redirect = drupal_get_destination();

And maybe turn 'alias' to FALSE (or remove it). See this for the drupal_get_destination() api.
---
Paul Craciunoiu

svdoord’s picture

I wonder if I am missing something with the following suggestion for improvement...

Instead of:

$redirect = 'destination='.drupal_get_path_alias(substr(urldecode(drupal_get_destination()), 12) );
# and then:
$login_link = l(t('Login'), 'user', array( 'query' => $redirect, 'alias' => TRUE ));

Couldn't you just do this?

$login_link = l(t('Login'), 'user', array( 'query' => drupal_get_destination(), 'alias' => FALSE ));

As I understand the meaning of the 'alias' parameter, it does exactly what you're trying to do by hand. The improvement would be in the part where you don't require urldecode() and substr(), which use knowledge about the implementation of drupal_get_destination(). That implementation may change in some future version.

The biggest advantage of my suggestion is that it nicely cooperates with global_redirect.

Namour4ever’s picture

Well... i have rewrited my whoe comment, because it was going realy long and has too much words for too few useful information. So:
I am using Drupal 6 and i want to force the user to login before acessing the content. i made a custom home page that redirect you depending of your status: if you are logged in, you'll go to http://dreamsgarden.toile-libre.org/index.php?q=node and if you are not, you'll go to http://dreamsgarden.toile-libre.org/index.php?q=user/login (http://toile-libre.org does not support clean urls).
The goal is to keep login page's layout (the "create a new account","login" and "i forgot my password" tabs, mainly), wich the login block does not have. The problem is that i can't deny content access to anonymous, because my custom home page will be acess-denied too, and therefore totaly useless.
I got a kind of workaround with this (i'm a newbie in php, and you will surely find a lot of improvements):

  • a block shown to anonymous users on all pages exept the login one (to avoid infinite loops while trying to enter the username/password) containing :
    global $user;
    // how do I say "if user is not logged in" ?
    //i got the ! from a Drupal 4,7 php snippet visibility code to show content to anonymous only
    if (!$user->uid) {
        echo '<meta http-equiv="refresh" content=0; http://example.com/index.php?q=login/user">'; }
    

(Hey, Drupal.org messed with my php code! why is there a "title=[...]" thing added?)
And the block shoots me "unexpected T_[something] on line 2" (i guess it's the "!")
so my plan was to use something like :

<meta http-equiv="refresh" content="0; http://dreamsgarden.toile-libre.org/index.php?q=user/login&destination=$url-you-were-before-being-redirected">

Any hint??
i see that you have explained the "&destination=" and ".get[...]" but i don't know how to appy it to my case since i'm not trying to add links to a block, but instead forcing my users to log in. Everybody's help is welcomed! (and yeah, i know, my english s**ks, so please... Be nice)

Namour4ever’s picture

Finally stopped ripping my head and used the standard login block with content access denied to anonymous users. Considering using logintoboggan.

bstrange’s picture

How can I remove the search link?

Whenever I remove

$search_link = l(t('Search'), 'search/node');
return $user_utilities . t(' | ') . $search_link;

I lose the rest of the links as well... Having search in this, for me, is kindof unnecessary as there are several other search locations on the site that no one ever utilizes

Edit to add:

Nevermind, figured it out, you have to leave
return $user_utilities;
but can 86 the redundant search functionality by deleting the remainder of the code I posted above.

Thanks for the nice snippet!