繁体   English   中英

登录后将用户带回上一页?

[英]Take user back to previous page after logging in?

我有一个名为Accounts的控制器,其中包含注册和注销视图。

相应的函数如下所示:

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
    ??? 
}

如果用户转到accounts/signin它首先检查表单是否被提交if(!empty($this->data))如果是,则将其登录,如果不是,则呈现登录表单。 如果他们成功登录,我想将他们重定向到登录页面之前的页面。

什么是最好的方法呢?

编辑:

我不认为我可以使用常规的http推荐人,因为从技术上讲,推荐人将始终是登录页面,因为他们转到/signin ,然后提交登录表单。 因此,在提交表单的位置,引用者始终/signin 我想重定向到他们之前的位置。 那有意义吗?

这是我如何用referer做的

我有2个登录表单,一个是所有页面顶部的表单,可以轻松登录,另一个是登录操作。 如果用户使用顶部的表单进入,则表单提交然后转到登录页面,您可以使用$this->referer()将用户重定向回来。

但问题是,如果用户输入错误密码或输入无效凭证,他将最终登录登录页面。 如果他然后输入正确的用户名+密码,并使用$this->referer()重定向,在这种情况下就是自己。 然后,用户可以1.再次重定向回登录页面或2.更糟糕的是可能陷入无限循环b / c登录将继续重定向到自身。

所以,我添加一些逻辑来检查以确保referer不是登录页面。 另外,我添加了另一个逻辑来存储$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') );
}

希望这对你有用。

http://book.cakephp.org/view/430/referer

使用包含初始引用者的隐藏<input>字段并使用登录数据提交。

我不知道最好的方法,但我将尝试的目标存储在会话变量中,然后将它们重定向到登录页面。

登录后,我将它们重定向到存储的目的地。

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.编辑您的登录表单。 我的是Users / 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']不是防弹的,但在这里我们讨论的是防止开放重定向漏洞,所以这就足够了。

使用AppController和UsersController进行设置

在AppController beforeFilter动作中

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

在UsersController登录操作中

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());                 
 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM