title: Redirect after login with community auth codeigniter
body: <p>I cannot get my head around how to redirect the user to the previous URL he was on after logging in.</p>
  
  <p>I have the following code in my controllers if the user does not have privileges to view it (so I redirect him to the login page):</p>
  
  <pre><code>//User is anonymous -&gt; go to login page (and remember the current URL for redirecting again after log in)
  $this-&gt;session-&gt;set_flashdata('referred_from', current_url());
  redirect(site_url( 'login?redirect=user', $redirect_protocol));
  </code></pre>
  
  <p>My login function in the Auth controller looks like this:</p>
  
  <pre><code>public function login()
      {
          // Method should not be directly accessible
          if ($this-&gt;uri-&gt;uri_string() == 'user/login/login')
              show_404();
  
          if (strtolower($_SERVER['REQUEST_METHOD']) == 'post')
              $this-&gt;require_min_level(1);
  
          $this-&gt;setup_login_form();
  
          // Passing Variables
          $data['title'] = lang('page_title_login');
          $data['class'] = 'login';
          $data['description'] = lang('meta_desc_login');
          $current = $this-&gt;namemanager-&gt;getBreadName("login");
          $data['bread'] = $this-&gt;breadcrumbmanager-&gt;getBreadcrumb($current);
          $data['loadGoogleApi'] = true;
          $data['loadFacebook'] = true;
  
          $data['referred_from'] = $this-&gt;session-&gt;flashdata('referred_from');
  
          // Template declaration
          $partials = array('head' =&gt; '_master/header/head', 'navigation' =&gt; '_master/header/navigation', 'content' =&gt; 'user/login_form', 'footer' =&gt; '_master/footer/footer');
          $this-&gt;template-&gt;load('_master/master', $partials, $data);
      }
  </code></pre>
  
  <p>Now when i do a var_dump on the referred_from, this does contain the correct value. </p>
  
  <p>However, my login view contains the following form open code:</p>
  
  <pre><code>echo form_open( $login_url, ['class' =&gt; 'std-form'] );
  </code></pre>
  
  <p>The $login_url is apparently set in the Auth_Controller.php file, being:</p>
  
  <pre><code>protected function setup_login_form( $optional_login = FALSE )
      {
          $this-&gt;tokens-&gt;name = 'login_token';
  
          /**
           * Check if IP, username, or email address on hold.
           *
           * If a malicious form post set the on_hold authentication class 
           * member to TRUE, there'd be no reason to continue. Keep in mind that 
           * since an IP address may legitimately change, we shouldn't do anything 
           * drastic unless this happens more than an acceptable amount of times.
           * See the 'deny_access_at' config setting in config/authentication.php
           */
          if( $this-&gt;authentication-&gt;on_hold === TRUE )
          {
              $view_data['on_hold_message'] = 1;
          }
  
          // This check for on hold is for normal login attempts
          else if( $on_hold = $this-&gt;authentication-&gt;current_hold_status() )
          {
              $view_data['on_hold_message'] = 1;
          }
  
          // Display a login error message if there was a form post
          if( $this-&gt;authentication-&gt;login_error === TRUE )
          {
              // Display a failed login attempt message
              $view_data['login_error_mesg'] = 1;
          }
  
          // Redirect to specified page
          /* Original Redirect
          $redirect = $this-&gt;input-&gt;get('redirect')
              ? '?redirect=' . $this-&gt;input-&gt;get('redirect')
              : '?redirect=' . config_item('default_login_redirect');
          */
  
          $redirect = $this-&gt;input-&gt;get('redirect')
              ? '?redirect=' . config_item('default_login_redirect')
              : '?redirect=' . config_item('default_login_redirect');
  
          // If optional login, redirect to optional login's page
          if( $optional_login )
          {
              $redirect = '?redirect=' . urlencode( $this-&gt;uri-&gt;uri_string() );
  
              $view_data['optional_login'] = TRUE;
          }
  
          // Set the link protocol
          $link_protocol = USE_SSL ? 'https' : NULL;
  
          // Load URL helper for site_url function
          $this-&gt;load-&gt;helper('url');
  
          // Set the login URL
          $view_data['login_url'] = site_url( LOGIN_PAGE . $redirect, $link_protocol );
  
          $this-&gt;load-&gt;vars( $view_data );
      }
  </code></pre>
  
  <p>When I go to my browser and I click on the login button, the URL automatically looks like this:</p>
  
  <pre><code>http://localhost/codeigniter/login?redirect=user
  </code></pre>
  
  <p>What do I need to do in order to redirect to the referred_from variable (in case this is filled in)? </p>