dongmei3869 2013-08-15 13:35
浏览 66
已采纳

CakePHP和用户登录功能

Here is a simple CakePHP login function (example taken from the CakePHP cookbook):

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $message = 'Username or password is incorrect';
            $this->Session->setFlash(__($message), 'default', array(), 'auth');
        }
    }
}

During testing of this login function I found out that:

if ($this->Auth->login()) {
    // ...
}

It lets a user to login even if authorization was done earlier. For example, if I logged in as User1 and without a call to logout function I'm trying to log in as User2 - I will get the next error:

Notice (8): Undefined index: User [APP/Controller/UsersController.php, line 83]

In this case I can hide a login form from the user. Is it a correct way?

Update: what can you say about the next code snippet:

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->loggedIn()) {
            $this->Auth->logout();
        }
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $message = 'Invalid login or password';
            $this->Session->setFlash(__($message), 'default', array(), 'auth');
        }
    }
}
  • 写回答

2条回答 默认 最新

  • dongzhimeng2464 2013-08-30 19:28
    关注

    The tutorial Simple Acl controlled Application - part 2 in the cookbook suggests that you read out the data with SessionComponent.

    You can also use the AuthComponent to check whether a user is already logged in or not. Use $this->Auth->user() in your controller. You can also pass a key to the first parameter to get a specific column of your users table or skip it to get all of the user's information. It returns Null if the user is not logged in or the key does not exist.

    Your login method could look like the following one (additions marked with a plus + and SessionComponent is used):

    public function login() {
    +   if ($this->Session->read('Auth.User')) {
    +       $this->Session->setFlash('You are logged in!');
    +       return $this->redirect($this->Auth->redirectUrl());
    +   }
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirectUrl());
            } else {
                $message = 'Username or password is incorrect';
                $this->Session->setFlash(__($message), 'default', array(), 'auth');
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系