Question

Goal:

When I am on any url and I use functions like login, logout, post_Comment, etc. I want the URL to stay untouched.

Example:

Few parts of the code will be written in pseudo code, just to give a simple example of what is being done inside the function. Everything that is important to my question will be in actual syntax.

Controller

public function index() {
  $this->load->view('main_v');
}

public function detail($id){
  //db select query for car details with given id
  //$data = query result
  $this->load->view('detail_v', $data);
}

public function login($name, $password){

  //find the user in db using the arguments and store his id in $user_id
  $data['user_id'] = $user_id;
  //PROBLEM: load the page/view/url from which the function was triggered
}

View *main_v*

  <a href="<?php echo base_url('controller/detail/3') ?>">
    <p>Display detail for car #3</p>
  </a>

View *detail_v*

  //car details

  <?php echo form_open('core/login'); ?>
    //input name
    //input password
    <p>Click here to log in</p>
  </form>

The Issue

Now as you can see the problem is in the login function. I would like to know how I could make it so that when the login function is called it does it's thing and then reloads the on which it was triggered.

What I have tried

I tried storing the name of the current view inside a session. I stored the name of the view in the session each time a view was changed. Then at the end of the login function I had this->load->view($this->session->userdata('current_view');. The result is only half what I need. While it displays the contents of the page on which I have triggered the login function it won't change the URL. Say, if I was @ index.php/controller/detail/3 and I clicked the Click here to log in my URL changed to index.php/controller/login, but the contents of the page were just like before. Second problem with this solution is that when you go BACK using the browser's navigation button it won't trigger the function that pulled that page and so inside my session I would still have current view set to the new page.

Second thing I've tried was putting $this->detail(3) instead of loading the view inside the login function, just to see what that does. The result is the same as before, I only get the contents, but the URL is still index.php/controller/login.

Final Word

I hope this made some sense. Thank you all for reading and please, please, share all the suggestions you have. I'll be looking forward to them.

Was it helpful?

Solution

Well this is a simple problem of 'redirecting back to original page'. You are thinking it through correctly but you fail by misunderstanding view vs redirect().

Process:

User lands on url /view/article/what-is-the-best. They are not logged in so they click the login button which shows the login form. The submit the form and are send to /account/login after a successful login.

What to change in the process:

When the user logs in you need to do the following to capture their referrer so load user_agent and capture the redirect into your session:

$this->load->library('user_agent');
$this->session->set_userdata('redirect_back', $this->agent->referrer());

So with that done, you have a session variable called redirect_back that holds the original page URL.

After login, you do a simple redirect (NOT loading a view since it keeps the same controller route):

redirect( $this->session->userdata('redirect_back') );

You could add something to check you get a referred too (in case of some oddity where you don't catch a referring url).

That should do it for you tho, get a user back to the original spot prior to login.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top