How to return to the referrer page in CakePHP after logging in

I had a problem returning to the referrer page:

first url: http://site.com/venue/apartments/1564/venue-name referer url: null 

On this page, I can edit the material inside, so I have a login button to go to the login page after a successful login, I would like to return to the first url .

 second url: http://site.com/users/login referer url: http://site.com/venue/apartments/1564/venue-name 

when I try to log in, due to CakePHP rules of action, I need to refresh the login page to verify credentials, here the problem starts:

 third url: http://site.com/users/login referer url: http://site.com/users/login 

refreshing the page and checking the credentials in the login action of the users controller, I get a link to the same page that I was on before, and now I can’t return to the first url .

I tried some solution by setting a session variable in the AppController, which checks if I am on the login action page and do this:

 AppController.php public function beforeFilter () { $this->setRedirect(); } public function setRedirect () { if ($this->request->params['controller'] != 'users' && $this->request->params['action'] != 'login') { $this->Session->write('redir', array('controller'=>$this->request->params['controller'], 'action'=>$this->request->params['action'], implode($this->request->params['pass'], '/'))); } } 

Checking var session with debug($this->Session->write('redir')); , everything will look perfect until I go to the login action:

 UsersController.php public function login () { if ($this->request->is ('post')){ if ($this->Auth->login()) { $redir = $this->Session->read('redir'); if (!empty($redir)) { $this->redirect ($redir); } else { $this->redirect ($this->Auth->redirect()); } } } } 

Performing this application interrupt, and if I debug($redir); var i get:

 array( 'controller' => 'js', 'action' => 'jquery', (int) 0 => 'plugin/jquery.validata.1.7.min' // just a jquery plugin loaded in default.ctp ) 

instead

 array( 'controller' => 'venue', 'action' => 'apartments', (int) 0 => '1564/venue-name' ) 

If I debug($redir); in any other representation of the whole site, everything works well, and I get the necessary data.

I thought of some sort of session protection procedure for the $this->Auth->login() action, but for me this is completely pointless.

How can I just redirect the user registered to the last page, which is not a login browse page?

+7
source share
3 answers

Have you tried this: $this->redirect(Controller::referer()); ?

I would have to re-read your entire question, but this should also work: $this->redirect( $this->referer() );

+15
source

I used sessions to access the last URL, and if the login is successful, the user will be redirected to it.

First, in AppController.php, I save the URL in the session, with the exception of the login and logout actions:

 public function beforeFilter() { $url = Router::url(NULL, true); //complete url if (!preg_match('/login|logout/i', $url)){ $this->Session->write('lastUrl', $url); } } 

In UserController.php for the login action, I have the following code:

 public function login() { if ($this->request->is('post')) { if ($this->Auth->login()) { if ($this->Session->read('lastUrl')){ $this->redirect($this->Session->read('lastUrl')); exit; }else{ return $this->redirect($this->Auth->redirect()); } } $this->Session->setFlash(__('Invalid username or password')); } } 
+11
source

I do it just like this:

 public function login() { if ($this->request->is('post')) { if ($this->Auth->login()) { $this->redirect($this->Session->read('referer')); } else { $this->Session->setFlash(__('Não foi possível efetuar o login. Usuário ou senha inválidos'), 'default', array('class' => 'alert alert-danger')); } } else { $this->Session->write('referer', $this->referer()); } } 
0
source

All Articles