title: Magento - Passing a session variable to a page called via _redirect()
body: <p>I have the need to pass a URL to Magento, where it should redirect the User after completing the logout. To store it, I do the following:</p>
  
  <pre><code>$BackTo = Mage::app()-&gt;getRequest()-&gt;getParam('backto');
  
  if(!empty($BackTo)) {
      Mage::getSingleton('core/session')-&gt;setBackTo($BackTo);
  }
  </code></pre>
  
  <p>When needed, I retrieve the URL using <code>Mage::getSingleton('core/session')-&gt;getBackTo()</code>. The issue is that, while this works well on login, it doesn't work on logout (where it's most needed). I can store the session variable, I can also immediately retrieve it, but, when I am in <code>logout.phtml</code>, where the redirect JavaScript is located, such variable is set to <code>null</code>.</p>
  
  <p>I suspect that the redirect performed by Magento upon logout has something to do with this "disappearing" session variable, but I can't say for sure.</p>
  
  <p>For completeness, here is the relevant code (there's more code than this, but they are mainly auxiliary functions, which don't get called on logout).</p>
  
  <p><strong>Account Controller</strong>  </p>
  
  <pre><code>class MyPackage_Redirectplugin_AccountController extends Mage_Customer_AccountController {
      /**
       * @see AccountController:logoutAction()
       */
      public function logoutAction() {
          $this-&gt;_getSession()
              -&gt;logout()
              -&gt;setBeforeAuthUrl(Mage::getUrl());
  
          // Store the "back to" URL in a session variable
          $this-&gt;StoreBackToURL();
  
          $this-&gt;_redirect('*/*/logoutSuccess');
      }
  
  protected function StoreBackToURL() {
      // Store the value of the "backto" argument, if it was passed
      $BackTo = Mage::app()-&gt;getRequest()-&gt;getParam('backto');
  
      if(!empty($BackTo)) {
          Mage::getSingleton('core/session')-&gt;setBackTo($BackTo);
          // At this point I can see the correct value stored in the session variable
      }
  }
  
  }
  </code></pre>
  
  <p><strong>Logout.phtml</strong></p>
  
  <pre><code>// The following command returns null
  $redirectURL = Mage::getSingleton('core/session')-&gt;getBackTo();
  </code></pre>
  
  <p>Thanks in advance for the help.</p>
  
  <p><strong>Update 12/09/25 - Found a workaround</strong></p>
  
  <p>Since I couldn't find a way to pass a session variable to the logout page opened by redirect, I chose an alternative way: I'm passing it via the URL. Specifically, I implemented a <code>logoutAction()</code> which calls <code>$this-&gt;_redirect('*/*/logoutSuccess', array('myvar' =&gt; $MyValue));</code>.</p>
  
  <p>In the template, where I have to do the redirect, I simply read such parameter using <code>Mage::helper('core')-&gt;urlDecode(Mage::app()-&gt;getRequest()-&gt;getParam('myvar'));</code>.</p>
  
  <p>I'm aware that there might have been better ways to implement the whole thing, but I needed a bug fix solution and this does the job. Thanks to all people who answered.</p>