description

This creates a little custom login area. If you are logged in, it displays your username and a link to your profile, otherwise it includes a Register and minimal Login area. It was originally developed for OurMedia.

If you want to customise the full page layout, click through to the Customising the login, registration and request password full page layout handbook page.

Put this code into the page.tpl.php file:

Drupal 5

Thanks, BooDy.

<?php  global $user; ?>
<?php if ($user->uid) : ?>
  <span class="login_text"><b>Logged in as: </span> <?php print l($user->name,'user/'.$user->uid); ?> |
  <?php print l("logout","logout"); ?>
    <?php else : ?>
<form action="user/login" method="post" id="user-login">
      <span class="login_text">Username:</span><span class="form-required" title="This field is required.">*</span>
      <input type="text" maxlength="60" name="name" id="edit-name"  size="5" value="" tabindex="1" class="form-text required" /></label>
      <label for="edit-pass"><span class="login_text">Password:</span><span class="form-required" title="This field is required.">*</span>
      <input type="password" maxlength="" name="pass" id="edit-pass"  size="5"  tabindex="2" class="form-text required" /></label>
      <input type="hidden" name="form_id" id="edit-user-login" value="user_login"  />
     <input type="submit" name="op" value="Log in"  tabindex="3" id="edit-submit" class="form-submit" /></form>
      <?php endif; ?>

Drupal 4.7

<?php  global $user; ?>
<?php if ($user->uid) : ?>
  <span class="login_text"><br><br><b>Logged in as: </span> <?php print l($user->name,'user/'.$user->uid); ?> |
<?php print l("logout","logout"); ?>
  <?php else : ?>
  <span class="login_text"><b>Login:</b> </span><form
action="user/login" method="post">
<div><div class="form-item">
<label for="edit-name"><span class="login_text">Username:</span><span class="form-required" title="This field is required.">*</span>
<input type="text" maxlength="60" name="edit[name]" id="edit-name"  size="10" value="" tabindex="1" class="form-text required" /></label>
<label for="edit-pass"><span class="login_text">Password:</span><span class="form-required" title="This field is required.">*</span>
<input type="password" maxlength="" name="edit[pass]" id="edit-pass"  size="10"  tabindex="2" class="form-text required" /></label>
<input type="hidden" name="edit[form_id]" id="edit-form_id" value="user_login"  />
<a href="/community/user/register">Register</a>&nbsp;&nbsp;<input type="submit" name="op" value="Log in"  tabindex="3" class="form-submit" /></form>
<?php endif; ?>
</div>

A variation that goes to the page the user is currently viewing.

  <div id="header-login">
    <?php  global $user; ?>
    <?php if ($user->uid) : ?>
    <?php print(t('Logged in as:'))?> <?php print l($user->name,'user/'.$user->uid); ?> |
    <?php print l((t('log out')),"logout"); ?>
      <?php else : ?>
    <form action="<?php print "{$base_path}user/login/?".drupal_get_destination();?>" method="post"><input type="hidden" name="edit[destination]" value="user" />
      <?php print l((t('Register')),"user/register"); ?> | <?php print(t('Login:'))?>
    <input type="text" maxlength="64" class="form-text" name="edit[name]" id="edit-name" size="15" value="" />
    <input type="password" class="form-password" maxlength="64" name="edit[pass]" id="edit-pass" size="15" value=""/>
    <input type="hidden" name="edit[form_id]" id="edit-form_id" value="user_login"  />
    <input type="submit" name="op" class="form-submit" value="<?php print(t('Login'))?>" />
    </form>
    <?php endif; ?> 
  </div>

Drupal 4.6

<?php  global $user; ?>
<?php if ($user->uid) : ?>
  Logged in as: <?php print l($user->name,'user/'.$user->uid); ?> |
<?php print l("logout","logout"); ?>
  <?php else : ?>
  <?php print l("Register","user/register"); ?> | Login: <form
action="user/login" method="post"><input type="hidden"
name="edit[destination]" value="user" /><input type="text"
maxlength="64" class="form-text" name="edit[name]" id="edit-name"
size="15" value="" /><input type="password" class="form-password"
maxlength="64" name="edit[pass]" id="edit-pass" size="15" value=""
/><input type="submit"  name="op" value="Log in"  /></form>
  <?php endif; ?>

description

function phptemplate_user_login($form) {
  return _phptemplate_callback('user_login', array('form' => $form));
}

Comments

ipwa’s picture

Thanks for this, it really helped me out, I like how this way you have total control of the login form, and is a really easy way to do it with out using functions.

I'd like to point out that there is small mistake for the Drupal 5 example. There is an evil < b > tag that doesn't get closed, I spent about an hour trying to debug my templates thinking it was some weird function I had done. I even asked JohnAlbin to login to my site to check it out, I'm buiding a zen-subtheme. He found the problem to be in my custom login block, and I was a bit embarrassed, but he was cool about it :).

Here's the Drupal 5 example updated:

<?php  global $user; ?>
<?php if ($user->uid) : ?>
  <span class="login_text"><strong>Logged in as: </strong></span> <?php print l($user->name,'user/'.$user->uid); ?> |
  <?php print l("logout","logout"); ?>
    <?php else : ?>
<form action="user/login" method="post" id="user-login">
      <span class="login_text">Username:</span><span class="form-required" title="This field is required.">*</span>
      <input type="text" maxlength="60" name="name" id="edit-name"  size="5" value="" tabindex="1" class="form-text required" /></label>
      <label for="edit-pass"><span class="login_text">Password:</span><span class="form-required" title="This field is required.">*</span>
      <input type="password" maxlength="" name="pass" id="edit-pass"  size="5"  tabindex="2" class="form-text required" /></label>
      <input type="hidden" name="form_id" id="edit-user-login" value="user_login"  />
     <input type="submit" name="op" value="Log in"  tabindex="3" id="edit-submit" class="form-submit" /></form>
      <?php endif; ?>

Or even better remove the strong tag and apply bold styles with CSS. Hope this saves someone some time.

Nicolas
-------------------------
http://nic.ipwa.net

Nicolas
-------------------------

FranCarstens’s picture

I'm using login_redirect. By changing
<form action="user/login" method="post" id="user-login">
to
<form action="/user?destination=login_redirect" method="post" id="user-login">
I was able to get this to work in D5

They should remove all the other scripts. This one is great.

-- If no-one asked Drupal questions there would be no Drupal answers --

taqwa’s picture

Thank you for writing this! I spent hours on this and went through forum page after forum page of the wrong advice until I found this. THANK YOU.