Wednesday, June 12, 2013

Dynamic login/log out link for Drupal 7

Because my logout/login link for Drupal 7 was so popular I've decided to post this piece of code that will include the username of the logged in user

<!-- Again, this php code goes in the page.tpl.php or in a block with php filter -->
<?php //this will display the username if the user is logged in
global $user; if ($user->uid) { 
print $user->name; } 
?>
<?php  //this is the same as the old code, printing a link to login or log out
global $user; if (!$user->uid) { 
print "<a href='http://".$base_path."/user'  class='logged out'>login</a>"; 

else {
print "<a href='http://".$base_path."/user/logout' class='logged in' >logout</a>";
 } 
?>

Hope you enjoy!