Custom login

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

<?php
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:

<?php

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

This shows only links:

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

Retaining destination page

spooky69 - April 13, 2007 - 09:18

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.

Login/logout and return to current page

zarko - June 22, 2007 - 01:31

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

<?php
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:

<?php
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);
?>

A good idea to remove

ttosttos - March 25, 2008 - 02:01

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

Rewrite of login block to create a custom utilites menu

istar100 - April 14, 2008 - 15:58

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

<?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. ' ' . $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;
?>

Great snippet just a tiny code correction...

udig - May 29, 2008 - 10:53

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');

Minor edits

EHA - June 3, 2008 - 03:01

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;
?>

D6 improvement

einsicht - July 25, 2008 - 20:34

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:

<?php
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:
<?php
$redirect
= drupal_get_destination();
?>

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

Can somebody tell me where I

davidliechti - August 28, 2008 - 10:15

Can somebody tell me where I have to paste all that code? In the user.module?

--
david liechti - switzerland

Improvement?

svdoord - September 1, 2008 - 15:39

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

Instead of:

<?php
$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?

<?php
$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.

 
 

Drupal is a registered trademark of Dries Buytaert.