Home  >  Article  >  Backend Development  >  cakephp user login verification example short good example

cakephp user login verification example short good example

黄舟
黄舟Original
2016-12-20 09:38:271038browse

/app/controllers/user_controller.php

class UserController extends AppController {
var $uses=array('user');
function index(){
  
}
function login(){
   if(!empty($this->data['user'])){
    //$this->user->name=$this->data['user']['name'];
    $user=$this->user->find("user.name='".$this->data['user']['name']."'");
    print_r($user);
    $user=$user['user'];
    print_r($user);
    if($user['password']==md5($this->data['user']['password'])){
     $this->Session->write('login',1);
     $this->flash('login suss!!','/user');
    }else{
     if(!$user['id']){
      $this->flash('no this user!!','/user');
     }else{
      $this->flash('name or pass is wrong!!','/user');
     }
    }
   }else{
    $this->flash('need name and password!!','/user');
   }
   exit();
}
function logout(){
   if($this->Session->check('login')){
    $this->Session->del('login');
   }
   $this->flash('logout ok ','/user');
   exit();
}
function reset(){
   $name='aaa';
   $pass='aaa';
   $user=$this->user->findByName($name);
   if($user['user']['id']){
    $this->user->set($user);
   }else{
    $this->user->set('name',$name);
    $this->user->set('password',md5($pass));
   }
   $ret=$this->user->save();  
   if($ret){
    $this->flash('update ok!!','/user');
   }else{
    $this->flash('update ok!!','/user');
   }
}
}
?>

/app/models/user.php

class User extends AppModel {
var $name="user";
var $useTable='users';
}
?>

/app/views/user/index.view  

controller->Session->read('login')!=1):?>
formTag('/user/login');?>
input('user/name');?>

password('user/password');?>

submit('submit');?>


简单的用户验证   试用$model->findByFields();   可以用model表中任意字段查询
$model->set($data);
$model->set($key,$value);
可以将查询结果直接set 后 save   如果有 主键值 会update ,如果没有 会insert到表里面。

以上就是cakephp 用户登录验证实例 短型好例子的内容,更多相关内容请关注PHP中文网(www.php.cn)! 


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn