Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
What is the best way to redirect user to previous page
  • I am keeping a session variable of Input::referrer(); ... this gets me the whole URL including http//server/blabla. Is this the best way to redirect user to that page or is there a way to get the controller/action/parameter segments from referrer to use that for redirection?
  • Hello, I don't understand how to put this code in my script :
    <?php
    use Fuel\Core\Session;
    use Fuel\Core\Lang;
    
    use \Process;//Package qui reprend tous les traitements
    use Process\Crypt;//Package qui reprend tous les traitements
    use Process\Format_data;//Package qui reprend tous les traitements
    use Process\Cmd;
    
    class Controller_Norm extends Controller_Template
    {
    
     public function action_index($nni=null)
     {
      if(!is_null($nni)){//Create a SESSION variable
       Session::set('nni',$nni);//Crée une variable de session 
      } else {
       Response::redirect('norms/404');
      }
       
      $data['versions'] = Format_data::get_all_version();
      $data['services'] = Format_data::get_all_service();
      
      $this->template->title = Lang::get('title');
      $this->template->content = View::forge('norm/index',$data); 
     }
     
     public function after($response){
      Session::set('test', 'test);
    
      return $response;
     }
     
     public function action_404()
     {
      return Response::forge(ViewModel::forge('norm/404'), 404);
     }
    }
    

    When I copy this method in my Controller, I have the follow error : Fuel\Core\FuelException [ Error ]: Controller_Norm::action_index() or the controller after() method must return a Response object. Normaly, I can use my Session variable in my other Controller ? Thank
  • Read the error. This has nothing to do with sessions, but with the fact that the action_index() method doesn't return anything. In this case because you have overloaded the after() method, but don't call the parent::after(), so there is no response. See the documentation.
  • Hello harro, Ok. I use the fuelphp version 1.2. I downloaded and tested the version 1.3. It's ok. However, I just want clarify the fonctioning of method before() and after($response).
    By default, they call implicity, by controller, for each method action_().
    They exists just for explode the method action_(). They allow to regroup different action of method. They have other reason for them existance ? Thanks
  • before() and after() are methods that are called, as their name implies, before and after the requested method is called. before() can be used as a prepping method for your actions, for example to setup a template, assign widget views, check for global controller access rights, etc. after() is used to post-process the results of your action. For example if you use the Theme class, you can assign the action method result to a theme partial and return the entire theme as a response. They should not be used for routing descisions, i.e. to alter the flow. If you need that, use the router() method to capture all action calls. Note that even with router(), before and after are called. They are not called by the controller, they are called by the Request class. If you don't need them, don't define them, all of them are optional.
  • Only if you set it yourself, you could save the last visited site uri to a session or cookie and use it for something like this. It's most certainly not part of the core as any solution would effectively be app-logic that has no place in the core.
  • I use a shutdown event for that, that (amongst other things) saves the current URI to a session variable. This way I always have the previous URI available. I am not using the referrer, as that can contain anything, from external URL's (to which I don't want to redirect, you'll lose your visitor) to potential dangerous links that are crafted by an attacker...
  • shutdown event?
  • See http://docs.fuelphp.com/classes/event.html The shutdown event is a system defined event, to execute tasks when Fuel is finished processing the request. It is called on the last line of your public/index.php file. Just use this in your code
    Event::register('shutdown', 'Myclass::mymethod');
    
    and your method in your class is called. Fuel uses the shutdown event internally to execute cleanup functions, and to flush the session...
  • I have one idea, I used to for my site. make base class like... class Controller_Base extends Controller
    {
    public function after($response)
    {
    if (isset($_SERVER))
    {
    $u = $_SERVER;
    else
    {
    $u = '/';
    }
    //
    Session::set('previous_uri', $u); return $response;
    }
    } and extend this class for your controller to handle web pages. Then when you go back to previous page, you can use Session::get('previous_uri') to get uri.
  • @Harro

    >> I use a shutdown event for that, that (amongst other things) saves the
    current URI to a session variable. This way I always have the previous
    URI available.
    I am not using the referrer, as that can contain anything, from external
    URL's (to which I don't want to redirect, you'll lose your visitor) to
    potential dangerous links that are crafted by an attacker...

    Can you show your implementation of redirect after authentication using shutdown event? I am not quite getting this to work for me?

    Thanks
  • Don't have access to code at the moment, but somewhere (for example in your app bootstrap) you do
    \Event::register('shutdown', function() { 
    \Session::set('previous_url', \Input::server('referer', null));
    });
    (note that you should check if the referer points to your side, to avoid redirecting away from your site)

    In your code you can then retrieve 'previous_url' from the session and redirect to it if not null.
  • Ok thanks I will try. I thought you have a solution that doesn't include a referrer as u mentioned above:

    "I am not using the referrer, as that can contain anything, from external
    URL's (to which I don't want to redirect, you'll lose your visitor) to
    potential dangerous links that are crafted by an attacker..."

    Anyway I don't think using $_SERVER['HTTP_REFERER'] is the right way to solve redirect after login problem. As far as I know HTTP_REFERER can be modified on user side and some browsers don't use it at all... Some notes about php referer here http://www.electrictoolbox.com/php-http-referer-variable/.
  • You are right, as said I just typed something in from the top of my head.

    You can use Uri::current() to get the current Uri and store that, the referer is even a bad idea because it's not the current URI, it's the one you came from...
  • I have come with following solution:
    - in my base controller in before method I set session variable with value Uri::string() for every page except login page
    - when logging in I read this session variable which will always hold the last page before coming onto login page


Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion