How to show logout button if user is logged in Drupal?

20,324

Solution 1

Take a look at this post on the drupal forum: Dynamic login/logout link in Primary Links

Solution 2

Simpler method. Make two items on the menu.

http://drupal.org/node/264225#comment-863102

Solution 3

Since Drupal is a CMS app, one could tweek this as administrator from the 'Stucture'->'Blocks' section. There you can find the 'User Menu' block. This is responsible for showing the logout fields. So make sure to enable it and to drag it to a visible area of your current theme. On the other hand, to be able to logout directly from the url, make sure you clear your browsers cache first. Eventually restart Apache ($ sudo /etc/init.d/apache2 restart) and then type http://yourdomain/?=logout. Should do the trick.

Solution 4

<?php
global $user;


if ($user->uid) 
{
Welcome:
print l($user->name,'user/'.$user->uid);
print l("logout","logout");//this is logout link
}
else 
{
//show him login form
}
?>

Solution 5

Quite easy, actually. Took me some time but I figured this out:

  • I use the module 'taxonomy access permissions' to grant or deny access to my nodes depending on user role.
  • Once installed, make taxonomy with terms 'logged in' and 'logged out', and make this taxonomy available for node type 'page'.
  • Administer the TAP module, setting permissions for anonymous user: logged in > deny deny deny, and logged out > allow deny deny. In the same way, set permissions for authenticated user: logged in > allow deny deny, and logged out > deny deny deny. Result: if I add a page and set the taxonomy to 'logged in', only authenticated users will see the page. If I set it to 'logged out', only anonymous users will see the page.
  • make a page with path 'log_in', add as content php code that redirects the user to path 'user' and set taxonomy to 'logged out'. When I'm not logged in, I can access the node and will be redirected to the login page. When I'm already logged in, I won't be able to access the node.
  • make a page with path 'log_out' and as content php code that redirects the user to path 'logout' and set taxonomy to 'logged in'. When I'm not logged in, I won't be able to access the node. When I'm already logged in, I can access the node and will be logged out.
  • in your menu, add two new items: one called 'LOGIN' with path 'log_in' and one called 'LOGOUT' with path 'log_out'.
  • Done! People not logged in will see the menu item 'LOGIN' but not 'LOGOUT' because access to that node is denied. People logged in will see the item 'LOGOUT' but not 'LOGIN' because access to that node is denied.
Share:
20,324
Tausif Khan
Author by

Tausif Khan

Updated on July 09, 2022

Comments

  • Tausif Khan
    Tausif Khan almost 2 years

    I need to show a logout button on my site if a user is logged in and a login button if a user is not logged in. How can I do that?

  • Dejan
    Dejan over 11 years
    Please elaborate on your answer by providing a summary not just link to the Drupal forum.
  • Fuzzy Analysis
    Fuzzy Analysis about 10 years
    You can also try yourdomain/?q=user/logout and yourdomain/?q=user/login for this type of emergency in more recent versions of drupal.