Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Problem with redirects
 
firewizard
Member
 
Avatar
Total Posts:  43
Joined:  2007-11-22
Bucharest, Romania
 

I was just testing some magento features, before going live with it, and I’ve noticed that some of the form submissions were displaying blank pages.
This was the case of Newsletter Subscribe and Poll Vote.

I went to Mage_Newsletter_SubscriberController and noticed this code in the preDispatch() method:

if ($referer $this->getRequest()->getServer('HTTP_REFERER')) {
            $this
->_referer $referer;
        
}

Everything ok, except not all browsers set the $_SERVER[’HTTP_REFERER’] property, making it unreliable. There are some ways to avoid a blank page (when a referer is not present - which was my case, using a standard Windows XP Firefox 2.0):
1. write a fallback, if no referer is present (i’ve used this one, as the messaging system works pretty well)

if ($referer $this->getRequest()->getServer('HTTP_REFERER')) {
                $this
->_referer $referer;
        
}
        
else {
                $this
->_referer Mage::getBaseUrl();
        
}
you could also use a dedicated page with a text or something

2. write a NavigationStack, which holds user’s viewed url’s on SESSION. Each time a page is loaded, it gets pushed in the stack (if it’s not already the last one). This way, when a form is submitted and you want to get back to the previous page, you can use NavigationStack::pop() or something alike.

Hope this helps.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Michael_1
Enthusiast
 
Total Posts:  826
Joined:  2007-08-31
 

Hi firewizard,

Actually we switched to using

$this->_redirectReferer();
in controllers.

That is defined in Mage_Core_Controller_Varien_Action as

/**
     * Set referer url for redirect in responce
     *
     * @param   string $defaultUrl
     * @return  Mage_Core_Controller_Varien_Action
     */
    protected function _redirectReferer($defaultUrl=null)
    
{
        $defaultUrl 
= empty($defaultUrl) ? Mage::getBaseUrl() : $defaultUrl;
        
$refererUrl $this->getRequest()->getServer('HTTP_REFERER');
        if (
$url $this->getRequest()->getParam(self::PARAM_NAME_REFERER_URL)) {
            $refererUrl 
$url;
        
}
        
if ($url $this->getRequest()->getParam(self::PARAM_NAME_BASE64_URL)) {
            $refererUrl 
base64_decode($url);
        
}
        
if (empty($refererUrl)) {
            $refererUrl 
$defaultUrl;
        
}
        $this
->getResponse()->setRedirect($refererUrl);
        return 
$this;
    
}

All the code that used the old way will be updated and available in the next release.

Thank you.
Michael.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top