Hi,

I want to customise login using the code on http://drupal.org/node/17272. I am new to Drupal. Can anyone tell me where I sould paste the code to? Where is the path field? Which file is it?

Thank you.

Comments

rszrama’s picture

Login and use the menu to go to administer->blocks. There will be an "add block" tab you must click on. Fill in the fields and copy and paste the code into the block body box. Open up the little Input Format field at the bottom and choose PHP. After you save it, you need to select it to be enabled and it's a go!

You will need to change the code there to work, as its paths are screwy. I'd use the l() function like so:

  global $user;
  if (!$user->uid) {
    // Change the following line's text to whatever you want.
    return l("Login/Register", "user/login");
  }
  elseif ($user->uid) {
    // The following line will display the username you are logged in as.
    return 'Logged in as ' . $user->name . '<br>' . l("Logout", "logout");
  }
Jennifersweet’s picture

Thanks rszrama, it works for me. However, I was wondering if it is possible to create just a text link instead of block. Is there any way to position the text link on the top-right corner of the page?

Thank you.

potential’s picture

You can also put that code into your page.tpl.php file

Put it into the header and position it with CSS.

Jennifersweet’s picture

Thanks, Potential. I put the code in page.tpl.php and positioned it with CSS as you said. However, the code doesn't work properly.

According to the code, the text link should appear as 'register/login' before logging in and change to 'login as user/logout' after the user logged in the site. However, it appears as 'register/login' even after I have already logged in the page. Can anyone tell me how to fix it?

Thank you.

rszrama’s picture

But at the moment my lunch break is almost over and I don't have the chance to test a solution.

The way the code determines whether or not to change is by accessing the object $user. If the code isn't executing like you're expecting, then I'm assuming perhaps you can't access $user from the template file? If not, the conditional "$user->uid" will always return false. Maybe there's some other way to determine if a user is logged in...

Like I said, I don't have the time to tweak the template file, but if no one else has solved the issue by the time I go home tonight, I'll give it a go and let ya know what I find.

rszrama’s picture

If you're using the bluemarine theme, open up page.tpl.php and find this part around line 22:

print theme('links', $primary_links)

Change it to something like so:

  print theme('links', $primary_links) . '<br>';

  global $user;
  if (!$user->uid) {
    // Change the following line's text to whatever you want.
    print l("Login/Register", "user/login");
  }
  elseif ($user->uid) {
    // The following line will display the username you are logged in as.
    print 'Logged in as ' . $user->name . '<br>' . l("Logout", "logout");
  }

You can tweak the formatting and what not, but that tested just fine for me.

Jennifersweet’s picture

I will try your method and let you know how it turns out. I was wondering if it works the same for other themes because I am using box grey at the moment and I might change to others in the future.

Thank you for your help.

bonaparte’s picture

The thread has been really helpful.

I wanted the register link along with the login link when the user is not logged in. I added one more line to suit my needs. I'm using these buttons on a custom region on top of the header. I added this code in the page.tpl.php file in the theme folder.

<?php

  global $user;
  if (!$user->uid) {
    // Print the login link
    print l("Login", "user/login") . " ";

    // Print the register link
    print l("Register", "user/register");
  }
  elseif ($user->uid) {
    // Print the logout link
    print  l("Logout", "logout");

  }
?>


--
Sudheer. S
Binary Vibes Information Technologies Pvt. Ltd.
www.binaryvibes.co.in | www.binaryvibes.in | www.lampcomputing.com | www.sudheer.net

michazero’s picture

I want to customise login and looked around for some hints. i fount some with the above node/17272 - but i cann't access it : "Access denied
You are not authorized to access this page." (i am logged in!)

please is there somebody who can give me this code to use it on my website?

Thank you.

hidetomo’s picture

This solved my problem in an instant...you guys are pure genius!

eff_shaped’s picture

bookmarking

rszrama’s picture

For what it's worth, you shouldn't just print $user->name like I was doing above... my how I've changed in 4 years. :-/

When you print a username, you should always wrap it in check_plain(), like so:

  print check_plain($user->name);