Magento Forum

   
Page 2 of 3
Remember me box on login page
 
spoonhonda
Jr. Member
 
Avatar
Total Posts:  29
Joined:  2009-02-15
Albany, Oregon, USA and Monterrey, NL, Mexico
 

Hey mike,
As I am not a programmer or very familiar with magento, I’m a little confused about how to do this.  Before I implemented your new code as described in your blog, I restored all the original Magento files that NemanjaS’s code had me modify.  Now after doing everything described in your blog I no longer see a the option to change the cookie lifetime setting in
system->config->web->Session Cookie User Management

Is your code an addition to NemanjaS’s code?
If so do I need to totally leave his code unmodified?

 
Magento Community Magento Community
Magento Community
Magento Community
 
miked2004
Sr. Member
 
Avatar
Total Posts:  214
Joined:  2007-12-13
Atlanta, Georgia
 

I noticed that my code still had the same problem. THe expiration date on the cookie did not get reset to the right exp date. I will try to work this out over the weekend.
As of now the code I posted is the same as NemanjaS’s except that it dosn’t modify any core files. No you you would not use my code and NemanjaS’s. I will post an update with working code as soon as i can.

 
Magento Community Magento Community
Magento Community
Magento Community
 
xondeo
Member
 
Total Posts:  40
Joined:  2009-04-08
 

Thank you for your work Mike! grin

 
Magento Community Magento Community
Magento Community
Magento Community
 
spoonhonda
Jr. Member
 
Avatar
Total Posts:  29
Joined:  2009-02-15
Albany, Oregon, USA and Monterrey, NL, Mexico
 

Yes, thanks you very much Mike! Sounds like if you solve the problem with your code you may also find a solution to fix the problem with Magentos core code. *crossing fingers*
Here is how my code looks in the “Sharpdot_Customcore.xml” and “config.xml” files.  Is my codes syntax correctly formed?

Create app/etc/modules/Sharpdot_Customcore.xml

<?xml version="1.0" encoding="utf-8"?>

<label>true</label>
local

Create app/code/local/{YourCompany}/etc/config.xml

<?xml version="1.0" encoding="utf-8"?>

<label>Sharpdot_CustomCore_Model_Cookie</label>
Sharpdot_Customcore_Block

 
Magento Community Magento Community
Magento Community
Magento Community
 
miked2004
Sr. Member
 
Avatar
Total Posts:  214
Joined:  2007-12-13
Atlanta, Georgia
 

@ spoonhonda: the code for app/etc/modules/Sharpdot_Customcore.xml is not correct. My xml tags were not being displayed for some reason. I am going to post the files so you can see the format. should be tonight around midnight.

The main problem is that the set cookie is called multiple times on the front end. So checking that remember me is set only works directly form the log in page. the next time that set cookie is called the remember me is not set so the expire time gets reset.

Hopefully going to have this worked out this weekend.

 
Magento Community Magento Community
Magento Community
Magento Community
 
spoonhonda
Jr. Member
 
Avatar
Total Posts:  29
Joined:  2009-02-15
Albany, Oregon, USA and Monterrey, NL, Mexico
 

Great, I am glad to hear you understand what the problem is.  Makes a lot of since how you explained it.  You the man!  I commend you for your efforts, very much appreciated.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Pixxa
Sr. Member
 
Total Posts:  275
Joined:  2008-10-23
 

@miked2004, Any progress? Your solution would be very welcome ! grin

 
Magento Community Magento Community
Magento Community
Magento Community
 
SLonghurst1
Member
 
Total Posts:  68
Joined:  2009-04-22
UK
 

I have a working remember me checkbox. Below are modified instructions from previous posts on how i got it working and how to write it so that it is not modifying core code.
1st

Create app/etc/modules/MyModule_all.xml

<?xml version="1.0" encoding="utf-8"?>

<MyModule_Core>
        <
active>true</active>
        <
codePool>local</codePool>
</
MyModule_Core>

Create app/code/local/MyModule/Core/Model/etc/config.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
    <global>
        <
models>
            <
core>
                <
rewrite>
                    <
cookie>MyModule_Core_Model_Cookie</cookie>
                </
rewrite>
            </
core>
        </
models>
    </global>
</
config>

Create app/code/local/MyModule/Core/Model/Cookie.php

<?php

class MyModule_Core_Model_Cookie extends Mage_Core_Model_Cookie
{
    
const XML_PATH_COOKIE_LIFETIME_LONG  'web/cookie2/cookie_lifetime2';

    public function 
getLifetime()
    
{
    
if (isset($_POST['rememberme'])){
    $lifetime 
Mage::getStoreConfig(self::XML_PATH_COOKIE_LIFETIME_LONG$this->getStore());
    return 
$lifetime;
    
}else{
        
if (null !== $this->_lifetime{
            $lifetime 
$this->_lifetime;
        
}
        
else {
            $lifetime 
Mage::getStoreConfig(self::XML_PATH_COOKIE_LIFETIME$this->getStore());
        
}
        
if (!is_numeric($lifetime)) {
            $lifetime 
3600;
        
}
        
return $lifetime;
    
}
    }
    

}

?>

main thing to note in the above and what i changed from the original posted file is the line

if (isset($_POST['rememberme'])){

in the original post it says to use

if (isset($_REQUEST['rememberme'])){

for me this did not work, but using POST did.

Next create app/code/local/MyModule/Core/Model/etc/system.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
  <
sections>
    <
web>
      <
groups>
            <
cookie2 translate="label">
                    <
label>Session Cookie User Management</label>
                    <
frontend_type>text</frontend_type>
                    <
sort_order>51</sort_order>
                    <
show_in_default>1</show_in_default>
                    <
show_in_website>1</show_in_website>
                    <
show_in_store>1</show_in_store>
                    <
fields>
                        <
cookie_lifetime2 translate="label">
                            <
label>Cookie Lifetime</label>
                            <
frontend_type>text</frontend_type>
                            <
sort_order>30</sort_order>
                            <
show_in_default>1</show_in_default>
                            <
show_in_website>1</show_in_website>
                            <
show_in_store>1</show_in_store>
                        </
cookie_lifetime2>

                    </
fields>
                </
cookie2
      </
groups>
    </
web>
  </
sections>
</
config>

Next add the following code to app/design/frontend/YourInstanceName/YourThemeName/template/customer/form/login.phtml

<div>
                    <
input type="checkbox" name="rememberme" value="remember"Remember me.
                </
div>

use the div block to style it how you like, make sure to place it in the html of the login form.

To test i set normal cookie lifetime to 60 seconds, new cookie lifetime to 360 seconds. If i log in and don’t select remember me I get logged off after a minute, if i log in and click the remember me box i get logged off after 6 minutes.

Thanks to all the previous posters for this made working out how to do it so much easier!

Shaun

 
Magento Community Magento Community
Magento Community
Magento Community
 
miked2004
Sr. Member
 
Avatar
Total Posts:  214
Joined:  2007-12-13
Atlanta, Georgia
 

@shaunlonghurst
In

if(null !== $this->_lifetime){
             $lifetime 
$this->_lifetime;
            
else {
                $lifetime 
Mage::getStoreConfig(self::XML_PATH_COOKIE_LIFETIME$this->getStore());
            
}

I get an error that : Notice: Undefined property:  Cookie::$_lifetime
It seems that you are trying to get the lifetime of the cookie. I was under the impression that you could not retrieve the lifetime of a cookie. You just had to set it again. Did you have to set anything else up so no errors were thrown when using

$this->_lifetime
?

 
Magento Community Magento Community
Magento Community
Magento Community
 
SLonghurst1
Member
 
Total Posts:  68
Joined:  2009-04-22
UK
 

Hi

No i didn’t get any errors it just worked. Have you set the cookie lifetime values in system -> configuration -> web ?

Shaun

 
Magento Community Magento Community
Magento Community
Magento Community
 
miked2004
Sr. Member
 
Avatar
Total Posts:  214
Joined:  2007-12-13
Atlanta, Georgia
 

For what ever reason I was not able to get shaunlonghurst’s code to work for me on ver 1.2.1

I did manage to get it working by adding an observer. If anyone else is having a problem let me know and ill post the way I did it. I did add one or two lines of code to the loginPostAction() in the core just to speed development. anyway. thanks to all who helped with this issue.

 
Magento Community Magento Community
Magento Community
Magento Community
 
spoonhonda
Jr. Member
 
Avatar
Total Posts:  29
Joined:  2009-02-15
Albany, Oregon, USA and Monterrey, NL, Mexico
 

Hey mike, would you be able to post the way you did it?  By the way your site look yummy!

 
Magento Community Magento Community
Magento Community
Magento Community
 
perky
Member
 
Total Posts:  44
Joined:  2009-12-13
 

Well friends

This is not working, Actually Cookie.php can not access the checkbox on login.php , isset($_post(rememberme)) or isset($_request(rememberme)) , They both can not check the value whether it is set or not.

Please if somebody got it’s solution than post ,

Thanks in advance

 
Magento Community Magento Community
Magento Community
Magento Community
 
perky
Member
 
Total Posts:  44
Joined:  2009-12-13
 

Well friends

This is not working, Actually Cookie.php can not access the checkbox on login.phtml , isset($_post(rememberme)) or isset($_request(rememberme)) , They both can not check the value whether it is set or not.

Please if somebody got it’s solution than post ,

Thanks in advance

 
Magento Community Magento Community
Magento Community
Magento Community
 
vivek291186
Jr. Member
 
Avatar
Total Posts:  8
Joined:  2010-01-26
Delhi
 

Step for Remember me functionality in login.phtml
Step 1:
For version magento-1.3.2.4: Please find the following path
http:\\yourdomain\app\code\core\Mage\Customer\controllers \AccountController.php
For version magento-1.4.1.0: Please find the following path
yourdomain\app\code\core\Mage\Customer\controllers\AccountController.php

Find following function:
public function loginPostAction()

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();
}

Find following function:
protected function _loginPostRedirect()
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 2:
Find following file
http:\\yourdomain\magento\app\design\frontend\default\default\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');
?>

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;" />
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;" />
Find following code in login.phtml
<input name="login[password]" type="password" class="input-text required-entry validate-password” id="pass" style="width:250px;" />

Replace with following code:
<input name="login[password]" type="password" class="input-text required-entry validate-password” id="pass" style="width:250px;" value="<?php if($cookie_user_password != ''){ echo $cookie_user_password;}?>” />

Find the line as follws:
<p class="required"><?php echo $this->__('* Required Fields') ?></p>
Add the following line after
<div> <input type="checkbox" name="rememberme" value="remember" /> Remember me. </div>

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 2 of 3