0

I have used ->isLoggedIn() method to detect whether a Magento user is logged in at that exact point. Now, I need to direct them to login IF they are not logged in, and specify a redirect URL upon success (which is not the same as the current URL - instead it will need a few extra parameters).

How do I do this? BTW we have Magento 1.4

I will be using:

Mage::getUrl(‘customer/account/login’);

to get the url, but it's how to redirect to a point that I don't know.

EDIT.. @Marius' post below was helpful and worked, but in some cases you may need to do a base64_encode yourself. essentially, whatever your login url is, make sure you add a trailing slash /, then add referer/nnnnnn where nnn is the base64_encode'd URL. To encode with JAVASCRIPT, I found this function, you can add it to your utilities:

https://stackoverflow.com/questions/246801/how-can-you-encode-a-string-to-base64-in-javascript to here: http://phpjs.org/functions/base64_encode/

base64_encode: function(data) {
    //  discuss at: http://phpjs.org/functions/base64_encode/
    // original by: Tyler Akins (http://rumkin.com)
    // improved by: Bayron Guevara
    // improved by: Thunder.m
    // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // improved by: Rafał Kukawski (http://kukawski.pl)
    // bugfixed by: Pellentesque Malesuada
    // example 1: base64_encode('Kevin van Zonneveld');
    // returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
    // example 2: base64_encode('a');
    // returns 2: 'YQ=='
    var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
        ac = 0,
        enc = '',
        tmp_arr = [];
    if (!data) return data;
    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);

        bits = o1 << 16 | o2 << 8 | o3;

        h1 = bits >> 18 & 0x3f;
        h2 = bits >> 12 & 0x3f;
        h3 = bits >> 6 & 0x3f;
        h4 = bits & 0x3f;

        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);
    enc = tmp_arr.join('');
    var r = data.length % 3;
    return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
}
2
  • can you post some code, or at least the area where you are checking (model, controller, block) ?
    – Marius
    Jun 26, 2015 at 11:39
  • I added the url-out code. BTW here is the link I got that from. It's the return I don't know how to do. mayankpatel104.wordpress.com/2012/06/27/… Jun 26, 2015 at 12:23

1 Answer 1

1

Let's say you want to redirect after login to the url Mage::getUrl('something/goes/here').

Do this:

$redirectUrl = Mage::getUrl('something/goes/here');
$encodedUrl = Mage::helper('core')->urlEncode($redirectUrl);
$loginUrl = Mage::getUrl(
     'customer/account/login', 
      array(Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME => $encodedUrl)
);

Then redirect to $loginUrl.
Just make sure you have config setting System->Configuration->Customer Configuration->Login Options->Redirect Customer to Account Dashboard after Logging in set to No.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.