Hello,
I'm new to drupal, but not to php. This is probably a common question, but is there a way of making a login/logout button in the primary menu? I noticed I wasn't allowed to code php in the 'primary link' location or name. I noticed I was able to select which users (logged in or guest) see the secondary links, but this doesn't seem to be the case for primary links.

Surely there's some way of doing this without any major hacks? I appologise if this has already been posted but I couldn't find it anywhere, or in the documentation.

Comments

Athaclena’s picture

In the hope there will be a useful answer (& applicable to Drupal 5 would be great).
I've searched for this and not found anything.

CleanCutRogue’s picture

Just make two separate menu options in the Primary Links menu... one that performs a login (call it "Log In" and let it path to "user/login") and one that perfoms a logout (call it "Log Out" and have it path to "logout"). Make their weights both -10 or something.

The core Drupal perms will only allow the logout link to appear when currently logged in, and the user/login link to appear only when not logged in. It will appear to toggle, but it's just being replaced as perms dictate.

Athaclena’s picture

Thank you so much!

armanschwarz’s picture

Great thanks!

fallenleaf’s picture

thank you very much, I find the way for long time, It works.
Any links is follow this rule? login and logout link in a block can smart filter?
---------------------
BUYERJOY

jaypabs’s picture

Thanks you so much for the information.

However I have a different menu that I want to make it visible in primary link and want to work it the same as Log In/Log Out. This menu is called "Submit Code". The path to this menu should be "/submit-code" if the member has already logged in. If it's not then it will directly go to "/user" to show the Log In form to the user.

Thanks in advance.

Download free source code

kris69’s picture

Hey thanks alot.Its working fine.

Anonymous’s picture

Thanks. It does a great job.

rivena’s picture

This also really helped me, I'm on Drupal 6. Thanks!

Anisa.

see15_aug’s picture

Thanks it also works for me :)

suresh7787’s picture

Excellant work boss. It help me a lot. Thank u so muchhhhhhhhhh!!!!!!!!!!!!

Suresh.E
Am a Acquia Certified Developer--Life Knocks us down. we have to decide whether or not to get up--

crosas’s picture

just what I was looking for ;)

thanks!

giorgio79’s picture

Just what I was looking for. Nice solution for a login button for anonymous users, and a logout for authenticated users :)

Cheers

bmherold’s picture

Is there a way to show the current logged-in user's username in the logout link? So if my username was 'brian' the link would read 'Logout of Brian'?

CleanCutRogue’s picture

Not in the standard Primary Links section, no. Not a way that I'm aware of. If you wanted to make your own menu in a block, you could place it there with some php code:

<?php
  global $user;
  print l('logout of '.$user->name, 'logout');
?>
vasheck’s picture


<?php global $user; 
	if ($user->uid == 0){
		print l(t('login'), 'user');
		}
	else{
	  print l(t('log out'), 'logout');
} ?>

santy5oct’s picture

Just what I was looking for. Nice solution for a login button for anonymous users, and a logout for authenticated users :)
This is very helpful to everyone.

Thanks

DineshBhatia’s picture

hello people new to Drupal not understanding as of now.

DineshBhatia’s picture

I am using Drupal 6.16
I have gone through number of pages in Drupal but still not understanding how to place
Login and Logout link. I have removed the login and logout menu.
Logging in works by simply typing the .../login in url and the login page opens.
Same is the case with logout. It logs out when i type logout in url and press [Enter] .

But when I try making the buttons on primary menu by placing the url path it says :::

"The path 'mydomain.com/logout' is either invalid or you do not have access to it." (for logout)
"The path 'mydomain.com/login' is either invalid or you do not have access to it." (for login)

Thanks.

itserich’s picture

For myself I entered only "logout" in path.

No domain.com/ Just logout.

thushad’s picture

 global $user;
    if ($user->uid == 0){
        print l(t('login'), 'user');
        }
    else{
      print l(t('log out'), 'logout');
} 

This is very usefull & can I get a 'My Profile' link next to 'logout' link for authenticated users.

Thanks.

ceejs’s picture

does this 'dynamic logout/login' link work for drupal 7? I am running 7 and I don't believe it does. I always have both 'Login' and 'Logout' main menu links.

i meant to reply to the main thread here. sorry...

jaypabs’s picture

If you will simply follow the code above:

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

You will now get both login/logout menu.

gamelodge’s picture

I believe the links in Drupal 7 Are:

Login: user/login
Logout: user/logout

elianarunner’s picture

the path solved my problem in drupal 7. Thanks!

Jorgas’s picture

Thanks a lot

irinaz’s picture

 global $user;
    if ($user->uid == 0){
        print l(t('login'), 'user/');
        }
    else{
      print l(t('log out'), 'user/logout');
}