Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I do this by extending my controller and I check in constructor if person is logged in or not, if person is logged in I save to the session current URL, and redirect person to the login page (if same constructor is applied (controller one) I make exception to not save current URL to the session) after logging in I call redirect function to the session variable.</p> <p>How to extend your controller is done here <a href="http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY" rel="nofollow">http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY</a> </p> <p>note that when your controller is extended you use <code>$this-&gt;data['variable_sent_to_view']</code> and you can omit second parameter of <code>$this-&gt;load-&gt;view()</code></p> <p>here is some example code assuming you know how your login controller works</p> <pre><code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class MY_Controller extends CI_Controller { function __construct() { parent::__construct(); $this-&gt;output-&gt;enable_profiler(FALSE); if ($refer = $this-&gt;session-&gt;flashdata('refer')) { $this-&gt;data['refer_page'] = $refer; // $this-&gt;data['refer_page'] is variable that you are interested in unset($refer); } else { $this-&gt;data['refer_page'] = base_url(); //default refer_page } //check if user is NOT logged in if (!$logged_in) { $this-&gt;_setRefer(); //this is private function } // else dont care about it } private function _setRefer() { $invalid_method = array('search', 'login'); // if method is 'search' or 'login' url will not save in session (it will stay same as was before) $valid_refer = TRUE; if (in_array($this-&gt;router-&gt;method, $invalid_method)) { $valid_refer = FALSE; } if (!(count($_POST) &gt; 0) &amp;&amp; $valid_refer === TRUE &amp;&amp; !$this-&gt;input-&gt;is_ajax_request()) { $this-&gt;session-&gt;set_flashdata('refer', current_url()); } else { $this-&gt;session-&gt;set_flashdata('refer', $this-&gt;data['refer_page']); } } } </code></pre> <p>now in after succesful login redirect to $this->data['refer_page'], but note that login controller must by extended by MY_Controller.</p> <p>this script also takes care about what happens if user made mistake and inserted wrong password (page will reload but "old" url stays)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload