Community & Support

Login / Logout Links (need help with my code)

Hey guys,

I need help with the following code for visitors to see "login | sign up | help" or for members logged in "username | logout | help" . I have a bit of code, but need help getting it to work inside the page.tpl.php file (phptemplate).

<div id="top-links">
<?php if ($user->uid == 0) : ?>
    <a href="/user">login</a>
    <a href="/user/register">sign up</a>
<?php else: ?>
    <?php echo "<a href=\"/user/$user->uid\">$user->name</a>"; ?>
    <a href="/logout">logout</a>
<?php endif; ?>
    <a href="/faq">Help</a>
</div>

Thanks,

Ivan
novice

Comments

I use this code

I use this code to do what you are attempting

<ul>
<?php global $user; ?>
<?php if ($user->uid): ?>
<li><?php print l('Logout', 'logout') ?></li>
<li><?php print l('My Account', 'user/'. $user->uid) ?></li>
<?php else: ?>
<li><?php print l('Login', 'user') ?></li>       
<?php endif; ?>
</ul>

modified code from above two

The code presented by rjl is nice and works well. There are two presentational differences - whereas Ivan desires that the registration link should also be presented - the code of "rjl" has additional feature that it also presents a link for account settings for logged user. Additionally Ivan needs to have a link for help there too in either case. I merged the features in both the codes and final working (4.7) ver is here

<?php
global $user;
if (
$user->uid == 0) {
    print
l('Login', 'user') . " ";
    print
l('Sign Up', 'user/register') . " ";
} else {
    print (
$user->name." : ");
    print
l('My Account', 'user/'. $user->uid) . " ";
    print
l('Logout', 'logout') . " ";
}
print
l('Help', 'user/help');
?>

for no logged user it presents
a) Login
b) Sign Up -> links to registration as desire by Ivan
c) Help

for logged in users the links become
a) username (no Link)
b) My Account -> links to profile as used by rjl
c) Logout
d) Help

Hope this suits most of the needs...

Regards,

rjl and rtandon, Thank You!

rjl and rtandon, Thank You! Code works great! -Ivan

Perfect but..

So much searching for this piece of code. How can it be adjusted so that it appears on all pages not just the home page?

That can depend on many

That can depend on many things...

But the usual place is in the theme's page.tpl.php file

Account Menu module

You might also try the Account Menu module.

signup not working

Hello!

Ive tried the code which u have given and its so impressive.
But there is bit prob with the signup link which is not exactly taking us to the registration page.

Hw do we fix this?

great help

thank you

Aimee @ BusinessPRnews.com
DFW Business Network

Websites:
BusinessPRnews.com
AIBusinessPro.com

Submit Button

Hi, i have the code working great. I was just wondering, is it possible to have the logout link as an actual button?

If you want the "look" of a

If you want the "look" of a button, that can be achieved pretty easily via CSS. Add a class to the link and define the style in your CSS file. To add a class using the l function (see api for more info http://api.drupal.org/api/drupal/includes--common.inc/function/l/7)

<?php
l
('My Account', 'user/'. $user->uid, array('attributes'=>array('class'=>'your-class-name-here')))
?>

If you want an actual submit button, that would require creating a form, which would take a bit more code. If you do want a form, can I ask why?

nobody click here