[英]how to show username when user is logged in with cakephp?
I'm new to cakephp and I can't show the username when a user is logged. 我是Cakephp的新手,登录用户时无法显示用户名。 here is my code from my default page: 这是我默认页面中的代码:
<?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; ?>
I'm lost and I don't see why the user is not considered as a variable? 我迷路了,我看不到为什么不将用户视为变量? thanks a lot for the help! 非常感谢您的帮助!
This is the exact line of codes I have used. 这是我使用过的确切代码行。 You have to change in controller. 您必须更改控制器。
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]);
}
}
}
Later you can use Auth class to show user information using 稍后,您可以使用Auth类通过以下方式显示用户信息
<?php echo $this->Session->read('Auth.User.username') ; ?>
Hope that helps 希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.