2

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.

Jakub
  • 20,418
  • 8
  • 65
  • 92
Cream Whipped Airplane
  • 1,305
  • 3
  • 13
  • 31
  • For the login, have you considered using a little bit of client side scripting to keep you on the same page while the login takes place? That's the most typical way folks go about it. Why are you doing this I must ask? – Zarathuztra Dec 30 '13 at 00:31
  • Does not calling the `login` method in and of its self perform a check to see if the user is logged in or not an redirect accordingly by default? – Ohgodwhy Dec 30 '13 at 00:43
  • I would like to get the original URL mainly for the sakes of when a user wishes to copy the URL and send it so someone. Obviously he needs to send `index.php/controller/detail/3` and not the `login`, but he just looses the original URL as soon as he runs any other function on the page.. By client side scripting you mean rewriting the URL with a script by providing it the original URL? – Cream Whipped Airplane Dec 30 '13 at 00:44
  • @Ohgodwhy I am not sure I understand. What do you mean by "by default"? – Cream Whipped Airplane Dec 30 '13 at 00:46
  • I apologize. I immediately assumed that you were using a codeigniter library such as tankauth/ionauth, but it would appear perhaps you've built this yourself. – Ohgodwhy Dec 30 '13 at 00:47
  • @Ohgodwhy oh, yes, I've build my own function. Don't apologize, should have made it more clear in the description. My bad. – Cream Whipped Airplane Dec 30 '13 at 00:50

1 Answers1

1

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.

Jakub
  • 20,418
  • 8
  • 65
  • 92
  • Oh my god! This works. And it is such a clean and simple solution. I love you man. I'll probably add one more check as you suggested whether the session data exists and if not the function will just load the main index, but this is perfect. I was googling for this for a day and not once did the user agent class popped out. Thank you. – Cream Whipped Airplane Dec 30 '13 at 10:03
  • Oh this is soooo great. I owe you one. You just made my coding experience 10x more enjoyable. – Cream Whipped Airplane Dec 30 '13 at 10:18
  • Glad it worked for you. Judging by your question tho, you need to read up on your *VIEWS*, *MODELS* & *CONTROLLERS* and what they do. Loading a view is not the right way to just 'send' people somewhere. Use the right tool for the job. – Jakub Dec 30 '13 at 20:29