cakephp会员注册及登录验证实例

一:首先数据库要有一张users表:

CREATE TABLE `users` (
  `id` int(10) NOT NULL auto_increment,
  `username` varchar(50) collate utf8_unicode_ci NOT NULL,
  `password` varchar(50) collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=4 ;

二 :创建注册register视图:在目录app/view/users下创建文件register.ctp,编辑代码如下:

<h3>Please fill out the form below to register an account.</h3>
<?php
    echo $form->create("User",array("action"=>"register"));//点击register后提交到users控制器里的action register方法处理
    echo $form->input("username");
    echo $form->input("password");//注意此处的字段要与数据库里的字段一致
?>
<?php
    echo $form->end("register");
?>

三:在app/controller目录下创建users_controller.php文件,在app/model下创建user.php文件:

users_controller.php代码如下:

<?php
class UsersController extends AppController
{
    var $name = "Users";
    var $helper = array("Html","Form");
  //会员注册功能模块
  function register()
  {
    if (!empty($this->data))
    {

        //对会员注册的密码使用md5加密
        $this->data['User']['password'] = md5($this->data['User']['password']);
      if ($this->User->save($this->data))
      {
        $this->Session->setFlash('Your registration information was accepted.');
        $this->redirect(array('action'=>'login'));

      } else {
        $this->Session->setFlash('There was a problem with your registration');
        $this->redirect(array('action'=>'register'));

      }
    }
  }
}
?>

user.php代码如下:

<?php
    class User extends AppModel{
        var $name = "User";

        //此处是对各字段规则校验,如果需要了解的可参考我的博文:cakephp如何实现数据校验

        var $validate = array(
            'username'=>array(
                array(
                    'rule'=>'notEmpty',
                    'message'=>'Username must not be empty!'
                ),

                array(
                    'rule'=>'isUnique',
                    'message'=>'Username must be Unique!'
                )
            ),

            'password'=>array(
                array(
                    'rule'=>'NotEmpty',
                    'message'=>'Password must not be empty!'
                ),

                array(
                    'rule'=>array('minLength','8'),
                    'message'=>'password must be at least 8 characters!'
                )

            )
        );
    }
?>

Ok,至此,如果cakephp其它的设置正确,用户点击注册后即将在数据库表users生成一对应的用户并将密码用md5加密;注册成功后跳转至login.ctp页面即登录页面;

四:现在继续完成登录及验证用户功能的实现:

先在/app/view/users下创建login.ctp,代码如下:

<h3>Please login.</h3>
<?php echo $form->create('User', array('action' => 'login')); ?>

<?php
    echo $form->input('username');
    echo $form->input('password');
?>
<?php echo $form->end('Login');?>

修改users_controller.php如下:

<?php
class UsersController extends AppController
{
    var $name = "Users";
    var $helper = array("Html","Form");
 
  function register()
  {
    if (!empty($this->data))
    {
        $this->data['User']['password'] = md5($this->data['User']['password']);
      if ($this->User->save($this->data))
      {
        $this->Session->setFlash('Your registration information was accepted.');
        $this->redirect(array('action'=>'login'));

      } else {
        $this->Session->setFlash('There was a problem with your registration');
        $this->redirect(array('action'=>'register'));

      }
    }
  }

 function login()
  {
    if($this->data)
    {

        //根据login.ctp页面上用户输入的用户名查找users表看是否有对应的用户:
        $results = $this->User->findByusername($this->data['User']['username']);

        //如果用户名存在,核对login.ctp页面上用户输入的密码用md5加密后是否与users表中的密码一致:
        if($results&&$results['User']['password'] == md5($this->data['User']['password']))
        {

            //用户名以及密码都正确,此时我们把用户名保存到session中,如果下次需要调用只需用$this->Session->read('user')
            $this->Session->write('user',$this->data['User']['username']);
            $this->redirect(array('action'=>'index'));
        }
        else
        {
            $this->Session->setFlash("Wrong username or password,please try again!");
            $this->redirect(array('action'=>'login'));
        }
    }
  }
}
?>

OK,至此登录页面及验证也完成,此文源于:http://my.oschina.net/adamboy

你可能感兴趣的:(cakephp会员注册及登录验证实例)