How I can show login link when user is not logged in, and logout link when the user is logged in?

Comments

Nguyen DO’s picture

you can check user object for logged in state

http://api.drupal.org/api/global/user

je1’s picture

The pages are in HTML, is there a way to mix php and html in one page.
Thx

ocamp’s picture

i think drupal should do it automatically,

if you create two links, login and logout the corect desitnations, i.e /logout, put them in any menu you like. And drupal will only show login if user is an anonymous user and will only show logout if the user is an authenticated user

cog.rusty’s picture

I guess you don't mean the existing user login block, which does that, but just a link which you can place wherever you want. Try this in your theme's page.tpl.php file, at the position that you want:

<?php print $user->uid ? l('Logout', 'logout') : l('Login', 'user/login'); ?>

About mixing html and php, you can see that the page.tpl.php template file is html containing php snippets. You can do the same in any post if you have enabled the "php filter" module and select the "php code" input format when editing that post. Or you can just "print" html inside the php.

je1’s picture

I looked at page.tpl and played with php in html but it didn't work?

New page, select full HTML filter, add print $user to the page, it display print $user

cog.rusty’s picture

If you did this in a node, notice what I wrote above about the "php filter" module and the "php code" input format.

Also, to make it work in a node you need to add a global $user; line before your code, otherwise $user will be considered a local variable which has no value.

je1’s picture

The filter if html because I have html code there and just need to insert php code in html?

This is test

print $user

I guess I need an example of php code in html code?

Also I tried menu link to logout/login, the logout works but the login doesn't?

vm’s picture

If you add php you have to use the PHP input format even when there is HTML in the node. Otherwise the php is stripped from display.

anytime php is added into a node, the php input format must be used to correctly run the php

cog.rusty’s picture

You can't run php code when the input format is not "php code". It just won't run. But you can enter html or anything you want when it is.

Also, $user is an object, not a sting, so you can't just print it. You can print $user->uid (user ID) or $user->name or $user->mail. To see what is available to print, you can do this:

print '<pre>';
print_r($user);
print '</pre>';
je1’s picture

Got it, thx guys