Welcome Guest, Not a member yet? Register   Sign In
Storing a request URL
#1

[eluser]JohnDoerther[/eluser]
Hi Guys,

First of all thanks a bunch for the great help so far - I am currently into my first steps on developing a website with CodeIgniter, and I absolutely love the framework.

I have noticed that, in a lot of cases, when I submit a form on a view I would just like to go back to the original page where the user filled out the form. This goes for a lot of things on the website I am creating, for example, the website includes a shopping cart - every time the cart is updated (I am using the Cart class...) I would just like to send the user back to where he came from.

Is there an easy way to do this? Can I tell a controller: "handle a user form, but when loading the view just go back to the original page".

This also goes for example for clicking a link that adds a product to a cart, deleting an item from a cart , ...

Thanks in advance
#2

[eluser]Twisted1919[/eluser]
The easiest way i can see here is to use the http referer .
You need to use a MY_Controller in order to achieve this .
so you will have something like :
Code:
class MY_Controller extends Controller{

   public $back_url ;

   function __construct()
   {
     parent::Controller();
     $this->back_url = (!empty($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']) !== FALSE) ? $_SERVER['HTTP_REFERER'] : site_url()
   }

}

//Then, your controller
class Process_form extends MY_Controller{
    
    function process()
    {
      if(not empty $_POST and other validations + database submissions)
      {
       redirect($this->back_url);
      }
    }

  }


Of course you can always store the last url into your session but is a bit more complex, for what you need, the above example should be enough .
#3

[eluser]JohnDoerther[/eluser]
Thanks for the quick response once again, I will try this out later and report back - thanks!
#4

[eluser]WanWizard[/eluser]
Argee, although the complex option is the safer option: if you keep it server side, you don't have a problem with brower plugins or proxies filtering the REFERER.

One option is to extend the session library:
Code:
class MY_Session extend Session
{
    function MY_Session()
    {
        parent::Session();
    }

    function sess_write()
    {
        $this->userdata['referer'] = current_url();   // assumes you have the url helper loaded

        parent::sess_write();
    }
}
#5

[eluser]Twisted1919[/eluser]
Wan, correct me if i am wrong , but the session is called before the controller action take place , doesn't this mean, that the current url will be always overwritten when making a new request, resulting in a "referer" of the current page always ? Meaning that if you do a $_POST to action/make_post then redirect[ redirect($this->session->userdata('referer')); ], will redirect to same action/make_post ? (of course i am talking about the method you explained above)
#6

[eluser]WanWizard[/eluser]
Ah, you're right.

I use a modified Session library that only writes the session once, at the end. I sometimes get confused between my code and standard CI, sorry.

You could do
Code:
class MY_Session extend Session
{
    function MY_Session()
    {
        parent::Session();
    }

    function sess_read()
    {
        $result = parent::sess_read();

        if ( $result )
        {
            if ( isset($this->userdata['current_url']) )
            {
                // the previous current_url is now the referer url
                $this->userdata['referer'] = $this->userdata['current_url'];
            }
            // store the current_url
            $this->userdata['current_url'] = current_url();   // assumes you have the url helper loaded
        }
        return $result;
    }
}
This only means that the first page request after a new session record has been created, you don't have a referer. For all subsequent requests, the previous url is now the referer.
#7

[eluser]Twisted1919[/eluser]
As i told before, it's a more complex way of achieving this, that's why i didn't started the discussion with session usage Tongue
#8

[eluser]haydenk[/eluser]
Sounds like everyone is letting it get more complex than it has to be. :|

The shopping cart is going to keep everything in the sessions and the way you describe it, you basically want to allow the user to "Continue Shopping" after adding product to their cart, so just figure out where you want to direct the customer (link wise) and put a link up there that says "Continue Shopping".




Theme © iAndrew 2016 - Forum software by © MyBB