如何在用户使用cakephp登录时显示用户名?

时间:2017-05-24 09:27:58

标签: javascript php cakephp cakephp-2.0

我是cakephp的新手,我无法在用户登录时显示用户名。 这是我的默认页面中的代码:

    <?php if ($loggedIn): ?>
    <p id="happytext"> Connecté en tant que </p>

    <!---Test--->
    <?= h($user->firstname) ?>
    //there I am supposed to show the name of the user but I always get 2 errors> Notice (8): Undefined variable: user [APP/Template/Layout/default.ctp, line 49]
    Notice (8): Trying to get property of non-object[ APP/Template/Layout/default.ctp, line 49]
    <!--En of test-->

    <a href="logout">Se déconnecter</a>
    <?php else: ?>
    <li class="right"><a href="register"><button id="register">S'enregistrer</button></a></li>
    <li class="right"><a href="login"><button id="login">Connexion</button></a></li>
    <?php endif; ?>

我迷路了,我不知道为什么用户不被视为变量? 非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

这是我使用的确切代码行。你必须改变控制器。

public function login() {
    $this->layout = 'login';

    if ($this->request->is('post')) {
        if ($this->__login_validation($this->request->data)[0] == '') {
            $params = array();
            $params[] = trim($this->request->data['user_name']);
            $pass = trim($this->request->data['user_pass']);
            $user = $this->User->callProcedure('UserGetPass', $params);
            if (!empty($user)) {
                $pass_stored = $user[0]['users']['pass_word'];
                if (empty($user)) {
                    $this->Session->setFlash('Please provide valid username and password');
                } else if (!empty($user)) {
                    $newHash = Security::hash($pass, 'blowfish', $pass_stored);
                    if ($user[0]['users']['username'] != trim($this->request->data['user_name']) || $user[0]['users']['pass_word'] != $newHash) {
                        $this->Session->setFlash('Please provide valid username and password');
                    } else {
                        $this->request->data = array(
                            'User' => array(
                                'username' => trim($this->request->data['user_name']),
                                'pass_word' => $pass
                            )
                        );
                        if ($this->Auth->login()) {
                            $this->__login_insert_history();
                            if ($this->Auth->user('user_type_id') == 99) {
                                $menus = $this->applicant_menu_list();
                            } else {
                                $menus = $this->user_wise_menus($this->Auth->user('id'));
                            }
                            $_SESSION['menus'] = $menus;
                        }
                    }
                } else {

                }
            } else {
                $this->Session->setFlash('Please provide valid username and password');
            }
        } else {
            $this->Session->setFlash($this->__login_validation($this->request->data)[0]);
        }
    }
}

稍后您可以使用Auth类使用

显示用户信息
 <?php echo $this->Session->read('Auth.User.username') ; ?>

希望有所帮助