这是我服务器上的ctp文件位置localhost / cakephp / view / login / index.ctp
<div class="wrapper">
<form class="form1" action="posts">
<div class="formtitle">Login to your account</div>
<div class="input">
<div class="inputtext">Username: </div>
<div class="inputcontent">
<input type="text" />
</div>
</div>
<div class="input nobottomborder">
<div class="inputtext">Password: </div>
<div class="inputcontent">
<input type="password" />
<br/><a href="#">Forgot password?</a>
</div>
</div>
<div class="buttons">
<input class="greybutton" type="submit" value="Cancel" />
<input class="orangebutton" type="submit" value="Login" />
</div>
</form>
</div>
用户控制器
class LoginController extends AppController {
var $helpers = array('Html', 'Form');
public function index() {
}
function login() {
// if the form was submitted
if(!empty($this->data)) {
// find the user in the database
$dbuser = $this->User->findByUsername($this->data['User']['username']);
// if found and passwords match
if(!empty($dbuser) && ($dbuser['User']['password'] == md5($this->data['User']['password']))) {
// write the username to a session
$this->Session->write('User', $dbuser);
// save the login time
$dbuser['User']['last_login'] = date("Y-m-d H:i:s");
$this->User->save($dbuser);
// redirect the user
$this->Session->setFlash('You have successfully logged in.');
$this->redirect('/posts/');
} else {
$this->set('error', 'Either your username or password is incorrect.');
}
}
}
function logout() {
// delete the user session
$this->Session->delete('User');
// redirect to posts index page
$this->Session->setFlash('You have successfully logged out.');
$this->redirect('/posts/');
}
}
我没有使用默认主题,而是自定义主题在view / themed / default上的位置。它正在发挥作用。 现在我无法登录auth所以请让我解决它。
答案 0 :(得分:1)
首先:你为什么不在意见中使用Form Helper?如果您正确配置它们,它将自动插入正确的值。
第二:您的表单指向“帖子”,而您想指向“用户/登录”。
最后:您的输入字段没有指定任何名称。这样,如果表单指向正确的控制器,控制器将永远不知道如何处理它。
我建议你阅读书籍教程'简单授权'。 http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
P.S。您没有指定CakePHP版本。我觉得它是2.X ......