Return user to previous page after login?

I have a controller called "Accounts" with the words "signin" and "signout".

The corresponding functions are as follows:

function signin()
{
    if (!empty($this->data)) 
    {
        //handle login
        ...
        //save login to session
        $this->Session->write('Account',  $data["Account"]);
        //redirect to previous page
        ???
    }
}

function signout()
{
    //delete login
    $this->Session->delete('Account');
    //redirect to previous page
    ??? 
}

If the user goes to accounts/signin, he first checks if the form has been submitted if(!empty($this->data)), if so, she registers them if he does not display the signing form. If they are successfully logged in, I want to redirect them to the page on which they were before the page with the signature.

What is the best way to do this?

Edit:

, HTTP-, signin , /signin, . , /signin. , . ?

+3
6

, , , - . , , $this->referer() .

, , . + , $this->referer(), . 1. , 2. . B/c login .

, , , . , $this->referer() , , .

, ( )

//get the current url of the login page (or current controller+action)
$currentLoginUrl = strtolower( "/" .$this->name ."/" .$this->action );
if( $this->referer() != $currentLoginUrl  )
{
    //store this value to use once user is succussfully logged in
    $this->Session->write('beforeLogin_referer', $this->referer($this->Auth->redirect(), true)) ) ;  //if referer can't be read, or if its not from local server, use $this->Auth->rediret() instead
}

, ( if( $this->Auth->user() ){ }):

//get the login page url again (by gettting its controller, or plural of Model, and this current page action and make the url)
$currentLoginUrl = strtolower( "/" .$this->name ."/" .$this->action );

//if the referer page is not from login page, 
if( $this->referer() != $currentLoginUrl  )
{
    //use $this->referer() right away
    $this->redirect($this->referer($this->Auth->redirect(), true));  //if referer can't be read, or if its not from local server, use $this->Auth->rediret() instead
}
else
{
    //if the user lands on login page first, rely on our session 
    $this->redirect( $this->Session->read('beforeLogin_referer') );
}

, .

+2

, , .

, .

+1

CakePHP 2.x

1. AppController.php

public function beforeFilter() {
        // redirect url
        if($this->request->here!= '/users/login') {
            $user_id = AuthComponent::user('id');
            if(empty($user_id)) { $this->Session->write('redirect_url_after_login', Router::url($this->request->here, true)); }
}

URL-, , URL- /users/login ( URL- ) .

2. . Mine /login.ctp. , .

    $redirect_url_after_login = $this->Session->read('redirect_url_after_login');
    if(!empty($redirect_url_after_login))
        echo $this->Form->input('redirect_url_after_login', ['value'=>$redirect_url_after_login, 'type'=>'hidden']);

3. , loginRedirect, .

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $redirect_url_after_login = $this->request->data['User']['redirect_url_after_login'];
            if(!empty($redirect_url_after_login)
                &&filter_var($redirect_url_after_login, FILTER_VALIDATE_URL)
                &&parse_url($redirect_url_after_login, PHP_URL_HOST)==$_SERVER['HTTP_HOST'])
                    return $this->redirect($redirect_url_after_login);
            $this->Session->delete('redirect_url_after_login');
            return $this->redirect($this->Auth->redirect());

}

, , " URL URL?" " ?".

. , $_SERVER['HTTP_HOST'] , , .

0

AppController UserController,

AppController beforeFilter action

$referer = Router::url($this->url, true);
$this->Auth->loginAction = array('controller'=>'users','action'=>'login','?'=>['referer'=>$referer]);

UserController login action

if($this->Auth->login())
{
 $this->Session->setFlash(__('Logged in successfully !'));
$redirect = $this->request->query('referer');
if(!empty($redirect))
 {
   return $this->redirect($this->Auth->redirectUrl($redirect));
 }else{
    return $this->redirect($this->Auth->redirectUrl());                 
 }
}
0
source

Source: https://habr.com/ru/post/1787731/


All Articles