4

I'm loading parts of the page via AJAX whose content includes a customer login link: Mage::helper('customer')->getLoginUrl()

Clicking the link gets the user to the login page and returns back to the HTTP referrer. Now that's the problem. Since the link is inside the content of an AJAX request the return/referer URL will be http://www.domain.com/ajaxhelper/ajax/custom_ajax/

How do I get the current URL outside the AJAX content? So when I load http://www.domain.com/ajaxhelper/ajax/custom_ajax/ via AJAX from http://www.domain.com/category-a.html I'd like to return http://www.domain.com/category-a.html instead of http://www.domain.com/ajaxhelper/ajax/custom_ajax/

Already tried this:

$session = Mage::getSingleton('customer/session');
$session->setBeforeAuthUrl(Mage::helper('core/url')->getCurrentUrl());
Mage::helper('customer')->getLoginUrl();

but as said Mage::helper('core/url')->getCurrentUrl() returns the url of the AJAX helper instead of the page invoking the AJAX request.

7

Instead of

Mage::helper('core/url')->getCurrentUrl()

you can use

Mage::app()->getRequest()->getServer('HTTP_REFERER');

This should give you the referrer of your current page (your current page being ajaxhelper/ajax/custom_ajax/). So you should have the URL of the page that did the AJAX call (request). I assume that is what you need.

  • Thanks for that, works like a charm. The only downside is that if someone disables referrers in their browser, this won't work, but that's okay in my scenario. If the referrer is empty, I'm just redirecting to the store home like this: $ref = (!empty(Mage::app()->getRequest()->getServer('HTTP_REFERER'))) ? Mage::app()->getRequest()->getServer('HTTP_REFERER') : Mage::helper('core/url')->getHomeUrl();. Another (more reliable) approach would be to just pass the current URL to the AJAX request as string, but using the HTTP referer is okay for me here. :) – oschloebe Aug 29 '13 at 8:08
  • On another note, using the Mage:registry approach on the catalog/product/view.phtml doesn't work as the Magento registry basically just sets global variables for the current request. Because the AJAX request is another request, the registry key will be empty inside the AJAX code. Just to let anyone know who tries it this way, too, and fails. :) – oschloebe Aug 29 '13 at 8:13
1

Try "referrer url" header... Please note, that there are some issues with that at ie, if you ok with that use it :-)

  • 5
    Perhaps you can explain your answer a little more using the OP code. – benmarks Aug 28 '13 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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