用PHP中的不同用户登录代码

时间:2016-04-22 04:46:43

标签: cakephp

我正在使用cakephp 2.x

表格字段:用户名,密码,acc_type

我只输入表单中的用户名和密码...

我想如果我在表单中输入用户名和密码,那么它会在表格中验证用户名和密码以及acc_type。

for ex - 如果username = raj且password = 123且acc_type = 1则会重定向到索引页。

对于前 - 如果用户名= raj且密码= 007且acc_type = 2则会明智地重定向到注册页面。

如果用户名和密码不正确,则显示错误...

以下是我的代码。

public function login() {  
        if (!empty($this->request->data)){         
            $this->User->set($this->request->data);

            if ($this->User->validates(array('fieldList' => array('username','password')))){

                $user = $this->User->find('first', array(
                    'conditions' => array(
                        'User.username' => $this->request->data['User']['username'],
                        'User.password' => md5($this->request->data['User']['password']),
                        'User.acc_type' => $this->request->data['User']['acc_type']                        
                    )
                )); 

                if((!empty($user)) && ($user.acc_type==1)){
                     $this->redirect(array('controller' => 'users', 'action' => 'index'));
                }
                elseif((!empty($user)) && ($user.acc_type==2)){
                     $this->redirect(array('controller' => 'users', 'action' => 'add'));
                }
                else
                {
                    $this->message('Invalid email / username or password');
                }
            }
        }    

    }

下面我用来在我的用户表中添加数据的函数..

public function add() {
    if ($this->request->is('post')) {   
        $this->User->create();
        if ($this->User->save($this->request->data)) {
            //print_r($this->request->data) ;exit();
            $this->Session->setFlash(__('The user has been saved.'));
            return $this->redirect(array('controller' => 'Users', 'action' => 'login'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
}

下面是我的型号代码

class User extends AppModel {

    public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A username is required'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A password is required'
            )
        ),
        'email' => array(
            'email' => array(
                'rule'    => array('email', true),
                'message' => 'Please supply a valid email address.'
            ),
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A email is required'
            )
        )

    );

    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {

            $this->data[$this->alias]['password'] = md5($this->data[$this->alias]['password']
            );
        }
        return true;
    }

}

查看代码

<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create(array('controller'=>'Users','action'=>'login','method'=>'POST')); ?>
<fieldset>
    <!-- <legend>
        <?php echo __('Please enter your username and password'); ?>
    </legend> -->
    <?php echo $this->Form->input('username');
    echo $this->Form->input('password');
    ?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?> 
 <?php echo $this->Html->link(__('Register'), array('action' => 'add')); ?>
</div>

2 个答案:

答案 0 :(得分:0)

试试这个

{{1}}

答案 1 :(得分:0)

也许你可以试试:

                $user = $this->User->find('first', array(
                    'conditions' => array(
                        'User.username' => $this->request->data['User']['username'],
                        'User.password' => md5($this->request->data['User']['password'])
                    )
                ));