CodeIgniter Forums
Sending user back to referrer - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Sending user back to referrer (/showthread.php?tid=11013)



Sending user back to referrer - El Forum - 08-22-2008

[eluser]Nial[/eluser]
I wanted to ask how people here handle this in CI/PHP: Basically, I have an AJAX powered function on my website. When a user clicks a link, Javascript is executed and an AJAX request is sent off. That's fine. However, in the interest of making my Javascript/AJAX degradable, the link will default to a standard URL when Javascript is not enabled.

The problem I'm having is figuring out how to return the user to the page that they were on before they clicked the link. In a nutshell:

User clicks link -> controller method is called -> database is updated -> return user to the page where they clicked the link.

The obvious solution would be to take the entire url and append it to the link URL, but I'm not sure how I'd parse the URL on the other side, given CI's URL structure.

Any suggestions?


Sending user back to referrer - El Forum - 08-22-2008

[eluser]JoostV[/eluser]
You could save last visited URL in a cookie or in the session.


Sending user back to referrer - El Forum - 08-22-2008

[eluser]erik.brannstrom[/eluser]
I use a library called History. More information in the following thread: http://ellislab.com/forums/viewthread/58091/. It's really good!


Sending user back to referrer - El Forum - 08-22-2008

[eluser]Bramme[/eluser]
You could simply put $this->session->set_flashdata('referrer', $this->uri->uri_strin()) in your construct and then use redirect($this->session->flashdata('referrer') in the method you need to redirect to...


Sending user back to referrer - El Forum - 05-26-2009

[eluser]rdjs[/eluser]
There were a couple of small typos in the code above. Nothing major but thought I would clarify to save confusion. Also current_url() was added in version 1.7 and would work nicely here:

To set the referrer in the flashdata:

Code:
$this->session->set_flashdata('referrer', current_url());

And to retrieve the referrer data

Code:
redirect($this->session->flashdata('referrer'), 'refresh');