title: CakePHP Login Redirect
body: <p>I have the following code in my login method:</p>
  
  <pre><code>    if(!(empty($this-&gt;data)) &amp;&amp; $this-&gt;Auth-&gt;user())
      {
  
          if($this-&gt;referer(array('controller' =&gt; 'users', 'action' =&gt; 'login')))
          {
              $this-&gt;redirect(array('controller' =&gt; 'home', 'action' =&gt; 'index'));
          }
          else
          {
              $this-&gt;redirect($this-&gt;Auth-&gt;redirect($this-&gt;referer()));
          }
      }
  }
  </code></pre>
  
  <p>This basically redirects the user to their previous page when they successfully login using a login form in the site header BUT if they login directly from the login page it will send them to the home page as they previous page would be the login form.</p>
  
  <p>The problem however is that if a user visits a page that requires authentication then they will be redirected to the login page and then login but because they have now visited the login page they will be redirected to the home page and NOT their previous page that they were trying to access.</p>
  
  <p>How do I fix this problem?</p>
  
  <p>Thanks</p>