2013-08-05 149 views
0

这是我的服务器localhost/cakephp中/查看/登录/ index.ctpcakephp登录无法正常工作。

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

CTP文件位置的用户控制器

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/'); 
} 

} 

我不使用默认的主题,但在自定义主题的位置视图/主题/默认。它正在工作。 现在我无法登录auth,请让我解决它。

回答

1

首先:你为什么不在你的视图中使用Form Helper?这将自动插入正确的值,如果你正确配置它们。

第二:您的表单指向'帖子',而您想指向'用户/登录'。

最后:您的输入字段没有指定任何名称。这样,如果表单指向正确的控制器,控制器永远不会知道该如何处理它。

我建议你阅读书籍教程'简单授权'。 http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

P.S.您没有指定CakePHP版本。我假装它是2.X ...