Remember me box on login page in magento

July 9, 2010 | In: Magento, web development

Steps for Remember me functionality in login.phtml

Step 1:
Please find the following path
http:\\yourdomain\app\code\core\Mage\Customer\controllers\AccountController.php

Step2:
Find following function:

public function loginPostAction()

and Replace with:
public function loginPostAction()
{
if ($this->_getSession()->isLoggedIn()) {
$this->_redirect(’*/*/’);
return;
}
$session = $this->_getSession();

if ($this->getRequest()->isPost()) {
$login = $this->getRequest()->getPost(’login’);
if (!empty($login[’username’]) && !empty($login[’password’])) {
try {
$_SESSION[’user_name’] = $login[’username’];
$_SESSION[’user_password’] = $login[’password’];
$session->login($login[’username’], $login[’password’]);
if ($session->getCustomer()->getIsJustConfirmed()) {
$this->_welcomeCustomer($session->getCustomer(), true);
}
}
catch (Exception $e) {
switch ($e->getCode()) {
case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
$message = Mage::helper(’customer’)->__(’This account is not confirmed. <a href=”%s”>Click here</a> to resend confirmation email.’,
Mage::helper(’customer’)->getEmailConfirmationUrl($login[’username’])
);
break;
case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
$message = $e->getMessage();
break;
default:
$message = $e->getMessage();
}
$session->addError($message);
$session->setUsername($login[’username’]);
}
} else {
$session->addError($this->__(’Login and password are required’));
}
}

$this->_loginPostRedirect();
}

Step3:
Find following function:

protected function _loginPostRedirect()

and Replace With following function:
protected function _loginPostRedirect()
{
$session = $this->_getSession();

if (!$session->getBeforeAuthUrl() || $session->getBeforeAuthUrl() == Mage::getBaseUrl() ) {

// Set default URL to redirect customer to
$session->setBeforeAuthUrl(Mage::helper(’customer’)->getAccountUrl());

// Redirect customer to the last page visited after logging in
if ($session->isLoggedIn())
{
if (!Mage::getStoreConfigFlag(’customer/startup/redirect_dashboard’)) {
if ($referer = $this->getRequest()->getParam(Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME)) {
$referer = Mage::helper(’core’)->urlDecode($referer);
if ($this->_isUrlInternal($referer)) {
$session->setBeforeAuthUrl($referer);
}
}
}
} else {
$session->setBeforeAuthUrl(Mage::helper(’customer’)->getLoginUrl());
}
}
//$customer12 = Mage::getModel(’core/cookie’);
//echo $lifetime = Mage::getStoreConfig(self::XML_PATH_COOKIE_LIFETIME_LONG, $customer12->getStore());

if(isset($_REQUEST[’rememberme’]) && $_REQUEST[’rememberme’] == ‘remember’)
{
$user_name = $_SESSION[’user_name’];
$pass_user_name = $_SESSION[’user_password’];
$cooki = Mage::getModel(’core/cookie’);
$cooki->set(’user_name’,$user_name);
$cooki->set(’pass_user_name’,$pass_user_name);
}
$this->_redirectUrl($session->getBeforeAuthUrl(true));
}

Step 4:
Find following file
http:\\yourdomain\magento\app\design\frontend\default\[your theme]\template\customer\form\login.phtml

Add following code top of login.phtml
<?php
$cooki = Mage::getModel(‘core/cookie’);
$cookie_user_name = $cooki->get(‘user_name’);
$cookie_user_password = $cooki->get(‘pass_user_name’);
?>

Step 5:
Find following code in login.phtml
<input name=”login[username]” value=”<?php echo $this->htmlEscape($this->getUsername()) ?>” title=”<?php echo $this->__(‘Email Address’) ?>” id=”email” type=”text” class=”input-text required-entry” style=”width:250px;” />

and replace with following code:

<input name=”login[username]” value=”<?php if($cookie_user_name != ”){ echo $cookie_user_name;}else{ echo $this->htmlEscape($this->getUsername()); } ?>” title=”<?php echo $this->__(‘Email Address’) ?>” id=”email” type=”text” class=”input-text required-entry” style=”width:250px;” />

Step 6:
Find following code in login.phtml
<input name=”login[password]” type=”password”pass” style=”width:250px;” />

and replace with following code:
<input name=”login[password]” type=”password”pass” style=”width:250px;” value=”<?php if($cookie_user_password != ”){ echo $cookie_user_password;}?>” />

Step 7:
Add the following line near submit button if it does not exits on the page:
<div> <input type=”checkbox” name=”rememberme” value=”remember” /> Remember me. </div>

That’s it