Embed login form on your own page

Last modified: April 6, 2007 - 20:52

Original post gives a link to the login page. Sometimes you may want to embed and blend the login form in your own page. If you want to display your own login form,modify this code to suit your need.

Drupal 4.6

<?php  global $user; ?>
<?php if ($user->uid) : ?>
Welcome: <?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?<?php print drupal_get_destination() ?>" 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="" />
<br>
<input type="password" class="form-password"
maxlength="64" name="edit[pass]" id="edit-pass" size="15" value="" />
<br>
<input type="submit" name="op" value="Log in" /></form>
<?php endif; ?>

Note: drupal_get_destination() is from user module. If you dont want redirection to current page after login, remove the php section along with the "?" preceeding it.

Drupal 4.7


Also note, that I access drupal under http://example.com/drupal ! You must change example above to wherever you set drupal home.

<?php  global $user; ?>
<?php if ($user->uid) : ?>
<span class="login_text">Welcome, </span> <?php print ($user->name); ?> <br>
<?php print l("Your Account",'user/'.$user->uid); ?> |
<?php print l("Log-Out","logout"); ?>
<?php else : ?>
<form action="/drupal/?q=user&amp;<?php print drupal_get_destination() ?>" method="post" id="user-login">
Username:<input type="text" maxlength="60" name="edit[name]" id="edit-name" size="15" value="" class="form-text required" />
<br>
Password: <input type="password" maxlength="" name="edit[pass]" id="edit-pass" size="15" class="form-text required" />
<br>
<input type="submit" name="op" value="Login" class="form-submit" />
<br>
<a href="/drupal/?q=user/password" title="Retrieve lost password">Retrieve lost password</a>
<input type="hidden" name="edit[form_id]" id="edit-user-login" value="user_login" />

</form>

<?php endif; ?>

if you want to stay on current page, just change this line:
<form action="/drupal/?q=user&amp;destination=user"  method="post" id="user-login">

with this
<form action="/drupal/?q=user&amp;<?php print drupal_get_destination() ?>"  method="post" id="user-login">

Drupal 5

<?php  global $user; ?>
<?php if ($user->uid) : ?>
<span class="login_text">Welcome, </span> <?php print ($user->name); ?> <br>
<?php print l("Your Account",'user/'.$user->uid); ?> |
<?php print l("Log-Out","logout"); ?>
<?php else : ?>
<form action="<?php print url($_GET['q'], drupal_get_destination())?>" method="post" id="user-login-form" >
Username:<input type="text" maxlength="60" name="edit[name]" id="edit-name" size="15" value="" class="form-text required" />
<br>
Password: <input type="password" maxlength="" name="edit[pass]" id="edit-pass" size="15" class="form-text required" />
<br>
<input type="submit" name="op" value="Login" class="form-submit" />
<br>
<a href="/drupal/?q=user/password" title="Retrieve lost password">Retrieve lost password</a>
<input type="hidden" name="edit[form_id]" id="edit-user-login" value="user_login" />

</form>

<?php endif; ?>

My Modification

Relentless - April 8, 2007 - 04:02

I had to make the following modifications to make it work
- change edit[name] and edit[pass]
- plus I didn't have any luck with the action="" so I changed it to /dp/user

it worked for me, but then again that might be a naive answer; I'm new at this...

<?php  global $user; ?>
<?php if ($user->uid) : ?>
<span class="login_text">Welcome, </span> <?php print ($user->name); ?> <br>
<?php print l("Your Account",'user/'.$user->uid); ?> |
<?php print l("Log-Out","logout"); ?>
<?php else : ?>
<form action="/dp/user" method="post" id="user-login-form" >
Username:<input type="text" maxlength="60" name="name" id="edit-name" size="15" value="" class="form-text required" />
<br>
Password: <input type="password" maxlength="" name="pass" id="edit-pass" size="15" class="form-text required" />
<br>
<input type="submit" name="op" value="Login" class="form-submit" />
<br>
<a href="/drupal/?q=user/password" title="Retrieve lost password">Retrieve lost password</a>
<input type="hidden" name="form_id" id="edit-user-login" value="user_login" />

</form>

<?php endif; ?>

This worked for me too, but

devnate - January 28, 2008 - 07:22

This worked for me too, but I also had to change the hidden field value from "user_login" to "user_login_block".

<input type="hidden" name="form_id" id="edit-user-login" value="user_login_block" />

user_login works for me

jallenb - June 18, 2008 - 20:36

This solution works well when action="https://drupal.host/user/login?destination=node" after removing global $user references for external sites/servers. Thanks!

Snippet from user/login Page Source

hatasaka - September 21, 2007 - 00:26

I cut this snippet directly from browser page source code for the Drupal 5.2 user/login page. I've left all div tags verbatim (I tidied up the formatting to study the logic). Drop this snippet into any 5x node and you should be good to go. I haven't gotten around to playing with the CSS formatting, but customizing appearances through style.css should be pretty straight forward.

<form action="/user/login?destination=login_redirect"
      method="post"
      id="user-login">
<div>
    <div class="form-item">
        <label for="edit-name">
            Username:
            <span class="form-required"
                  title="This field is required.">
                  *
            </span>
        </label>
        <input type="text"
               maxlength="60"
               name="name"
               id="edit-name"
               size="60"
               value=""
               tabindex="1"
               class="form-text required"
        />
        <div class="description">
            Enter your username.
        </div>
    </div>
    <div class="form-item">
        <label for="edit-pass">
            Password:
            <span class="form-required"
                title="This field is required.">
                *
            </span>
        </label>
        <input type="password"
               name="pass"
               id="edit-pass"
               size="60"
               tabindex="2"
               class="form-text required"
        />
        <div class="description">
            Enter the password that accompanies
            your username.
        </div>
    </div>
    <input type="hidden"
           name="form_id"
           id="edit-user-login"
           value="user_login"
    />
    <input type="submit"
           name="op"
           id="edit-submit"
           value="Log in"
           tabindex="3"
           class="form-submit"
    />

</div>
</form>

drupal_get_form('user_login_b

quadbyte - October 21, 2007 - 08:32

drupal_get_form('user_login_block');

Clean and works

lejon - February 8, 2008 - 17:07

The above didn't work for me in Drupal 5, so I took bits from here as well: http://drupal.org/node/17272

<?php global $user; ?>

<?php if ($user->uid) : ?>
<span class="login_text">Welcome, </span> <?php print ($user->name); ?> <br>
<?php print l("Your Account",'user/'.$user->uid); ?> |
<?php print l("Log-Out","logout"); ?>
<?php else : ?>

<?php print drupal_get_form('user_login_block'); ?>

<?php endif; ?>

Drupal 6.2 Modlification

wau - May 29, 2008 - 19:18

If you are using D6.2, you can use the following code in your block. Note that I am using the front_page module but you can modify your action destination accordingly.

<?php  global $user; ?>
<?php if ($user->uid) : ?>
<span class="login_text">Welcome, </span> <?php print ($user->name); ?> <br>
<?php print l("Your Account",'user/'.$user->uid); ?> |
<?php print l("Log-Out","logout"); ?>
<?php else : ?>
<form action="/drupal/?q=user&amp;destination=front_page"  method="post" id="user-login">
<table border=0><tr><td>Username:</td><td><input type="text" maxlength="60" name="name" id="edit-name" size="15" value="" class="form-text required" /></td></tr>
<tr><td>Password:</td><td><input type="password" maxlength="" name="pass" id="edit-pass" size="15" class="form-text required" /></td></tr></table>
<input type="submit" name="op" value="Login" class="form-submit" />
<br>

<input type="hidden" name="form_id" id="edit-user-login" value="user_login" />

</form>

<?php endif; ?>

Let Drupal produce the form markup

Matthew Davidson - June 6, 2008 - 05:30

A more Drupal-y solution would be this little bit of module code (replace MYMODULE with your module name; only tested on Drupal 5):

<?php
/**
* Implementation of hook_form_alter().
*/
function MYMODULE_form_alter($form_id,&$form) {
  switch (
$form['#id']) {
    case
'user-login':  
     
//allow use of login form from any page not produced by the user module without redirecting to user/$uid
     
if (arg(0) != 'user') {
       
$form['#redirect'] = $_GET['q'];
      }
      break;
  }
}

function
theme_MYMODULE_user_login () {
  global
$user;
  if (!
$user->uid) {
   
$output = '<h3>Login to My Cool Website</h3>';
   
$output .= drupal_get_form('user_login');
  }
  return
$output;
}
?>

This way you don't have do do any HTML form markup (ugh!), modifications other modules might make to the login form are preserved, and you can override the theme function in your theme's template.php to embellish the surrounding markup on a per-theme basis. Then to embed the form in your block/node/template/whatever, you just need:

<?php
print theme('MYMODULE_user_login');
?>

 
 

Drupal is a registered trademark of Dries Buytaert.