Friday, 3 June 2016

remember me (id-password) functionality on magento login page.

Magento\app\code\core\Mage\Customer\controllers\AccountController.php

Step: 2 Find following function.

protected function _loginPostRedirect()



Step: 3 Replace with following function code.

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'))
                 {
                       $referer = $this->getRequest()->getParam(Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME);
                       if ($referer)
                       {
                                 $referer = Mage::helper('core')->urlDecode($referer);

                                 if ($this->_isUrlInternal($referer))
                                 {
                                       $session->setBeforeAuthUrl($referer);
                                 }
                        }
                }
                else if ($session->getAfterAuthUrl())
                {
                        $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
                }
            }
            else
            {
                $session->setBeforeAuthUrl(Mage::helper('customer')->getLoginUrl());
            }

       }
       else if ($session->getBeforeAuthUrl() == Mage::helper('customer')->getLogoutUrl())
       {
             $session->setBeforeAuthUrl(Mage::helper('customer')->getDashboardUrl());
       }
       else
       {
             if (!$session->getAfterAuthUrl())
             {
                  $session->setAfterAuthUrl($session->getBeforeAuthUrl());
             }

             if ($session->isLoggedIn())
             {
                  $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
             }
       }

       if ($this->getRequest()->isPost())
       {

             $login = $this->getRequest()->getPost('login');
             $rememberme = $this->getRequest()->getPost('rememberme');

             try
             {

                  $cookie = Mage::getModel('core/cookie');

                  if (!empty($login['username']) && !empty($login['password']) && !empty($rememberme))
                  {
                        $cookie->set('user_name', $login['username']);
                        $cookie->set('pass_user_name', $login['password']);
                        $cookie->set('rememberme', 1);
                  }
                  else if (!empty($login['username']) && !empty($login['password']) && empty($rememberme))
                  {
                        $cookie->delete('user_name');
                        $cookie->delete('pass_user_name');
                        $cookie->delete('rememberme');
                  }
             }
             catch (Exception $e)
             {
             }
       }
       $this->_redirectUrl($session->getBeforeAuthUrl(true));
}


Step: 4 Find following file (login page functionality).

Magento\app\design\frontend\default\[your theme]\template\customer\form\login.phtml

Step: 5 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');

          $rememberme = $cooki->get('rememberme');

?>


Step: 6 Find following code in login.phtml (user id textbox).

<input type="text" name="login[username]" value="<?php echo $this->escapeHtml($this->getUsername()) ?>" id="email" class="input-text required-entry validate-email" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Email Address')) ?>" />

And replace with the following code. The code define the user id of user if that user select remember me option.

<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" class="input-text required-entry validate-email" type="text" />


Step: 7 Find following code in login.phtml (password textbox).

<input type="password" name="login[password]" class="input-text required-entry validate-password" id="pass" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Password')) ?>" />

And replace with the following code. The code define the password of user if that user select remember me option.

<input name="login[password]" type="password" class="input-text required-entry validate-password"  value="<?php if($cookie_user_password != ''){ echo $cookie_user_password;}?>" />

Step: 8 Add the following code at the place where you want to display checkbox for the functionality of remember me on page.

<div>
                            <input type="checkbox" name="rememberme" value="remember" <?php if($rememberme != ''){ echo "checked";} ?>  /> Remember me.

                        </div>

No comments:

Post a Comment