Login / Logout Links (need help with my code)
mudanoman - July 20, 2006 - 18:48
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

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