title: How to go back to referer page in CakePHP after login
body: <p>I have a problem on going back to referer page:</p>
  
  <pre><code>first url: http://site.com/venue/apartments/1564/venue-name
  referer url: null
  </code></pre>
  
  <p>In this page I can edit stuff inside, so I have a <code>login</code> button to go to the login page, after a successful login, I would like to turn back to the <code>first url</code>.</p>
  
  <pre><code>second url: http://site.com/users/login
  referer url: http://site.com/venue/apartments/1564/venue-name
  </code></pre>
  
  <p>when I try to login, due to the CakePHP action rules, i have to refresh the login page to check the credentials, the <code>problem</code> starts here:</p>
  
  <pre><code>third url: http://site.com/users/login
  referer url: http://site.com/users/login
  </code></pre>
  
  <p>by refreshing the page, and checking the credentials in the <code>login</code> action of <code>users</code> controller I get as refer the same page where I was before, and now I can't going back to the <code>first url</code>.</p>
  
  <p>I tried some solution by setting a session variable in the AppController which checks if I'm not in the <code>login</code> action page and doing this:</p>
  
  <pre><code>AppController.php
  
  public function beforeFilter () {
      $this-&gt;setRedirect();
  }
  
  public function setRedirect () {
      if ($this-&gt;request-&gt;params['controller'] != 'users' &amp;&amp; $this-&gt;request-&gt;params['action'] != 'login') {
          $this-&gt;Session-&gt;write('redir', array('controller'=&gt;$this-&gt;request-&gt;params['controller'], 'action'=&gt;$this-&gt;request-&gt;params['action'], implode($this-&gt;request-&gt;params['pass'], '/')));
      }
  }
  </code></pre>
  
  <p>By testing the session var with <code>debug($this-&gt;Session-&gt;write('redir'));</code> everything would seem to works perfect until I go to the <code>login</code> action:</p>
  
  <pre><code>UsersController.php
  
  public function login () {
      if ($this-&gt;request-&gt;is ('post')){
          if ($this-&gt;Auth-&gt;login()) {
              $redir = $this-&gt;Session-&gt;read('redir');
              if (!empty($redir)) {
                  $this-&gt;redirect ($redir);
              } else {
                  $this-&gt;redirect ($this-&gt;Auth-&gt;redirect());
              }
          }
      }
  }
  </code></pre>
  
  <p>Doing this break the application and if I <code>debug($redir);</code> var i get:</p>
  
  <pre><code>array(
      'controller' =&gt; 'js',
      'action' =&gt; 'jquery',
      (int) 0 =&gt; 'plugin/jquery.validata.1.7.min' // just a jquery plugin loaded in default.ctp
  )
  </code></pre>
  
  <p>instead of </p>
  
  <pre><code>array(
      'controller' =&gt; 'venue',
      'action' =&gt; 'apartments',
      (int) 0 =&gt; '1564/venue-name'
  )
  </code></pre>
  
  <p>If I <code>debug($redir);</code> in any other view of the entire site, everything works well and I get the right data.</p>
  
  <p>I thought in some kind of Session protection routine for <code>$this-&gt;Auth-&gt;login()</code> action but it's totally nonsense for me.</p>
  
  <p>How can I just redirect the user logged to the last page which isn't the <code>login</code> view page?</p>