php - 登录时密码不匹配

标签 php mysql codeigniter

我在注册表单时有以下代码:

function register()
    {
$this->form_validation->set_rules('txt_username', 'Username', 'trim|required');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|matches[txt_password]|md5');
$this->form_validation->set_rules('txt_password', 'Password', 'trim|required|md5');  
//other codes


$data = array('username' => $this->input->post('txt_username'),
              'password' => $this->input->post('txt_password')
            );

            // insert form data into database
           if ($this->account_model->insertUser($data));
             { 
            $this->session->set_flashdata('msg','successfully registered!');
            redirect('account/register');
        }
}

密码与此处匹配。 但是当我使用相同的数据用户名和密码登录时:它显示无效的用户名或密码: 登录功能如下:

function login()
{
                    //set validations
          $this->form_validation->set_rules("txt_username", "Username", "trim|required");
          $this->form_validation->set_rules("txt_password", "Password", "trim|required");

              $username = $this->input->post("txt_username");
              $password = $this->input->post("txt_password");

                $usr_result = $this->account_model->get_user($username, $password);

                    if ($usr_result > 0) //active user record is present
                    {
                    $this->account_model->login();
            $data['message'] ="You are logged in!"; 
        }
                    else
                    {
        $this->session->set_flashdata('msg', 'Invalid username and password!');
                    }
        }
}

我有一个用于将注册表数据插入数据库的模型:

function insertUser($data)
    {
        return $this->db->insert('user', $data);
    }

我有这个模型来检索数据以登录:

function get_user($usr, $pwd)
     {
          $sql = "select * from user where username = '" . $usr . "' and password = '".md5($pwd). "'";
          $query = $this->db->query($sql);
          return $query->num_rows();
     }

最佳答案

最好的方法instead of MD5是在Codeigniter中使用base64。这意味着decode()encode() .

<小时/>

编码

$password = 'mYp@ssw0rd'; // or $password
$encriptKey = 'super-secret-key';

$encrypted_string = $this->encrypt->encode($password, $encriptKey);

解码

$password = 'mYp@ssw0rd'; // or $password
$encriptKey = 'super-secret-key';

$decrypted_string = $this->encrypt->decode($password, $encriptKey);

此外

同时加载库$this->load->library('encrypt');

关于php - 登录时密码不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34713988/

相关文章:

php - 如何在Codeigneitor中同时向数据库插入多条记录

php - 根据页面url显示不同的侧边栏内容 - October CMS

php - 未捕获的类型错误 - 不是函数 - 没有视觉错误

mysql - 具有关系实体的 Entity Framework 原始 SQL

中等复杂度的 PHP 框架,介于 CodeIgniter 和 Yii 之间?

php - Codeigniter 表单验证 - 如何在成功后取消设置表单值?

php - htmlspecialchars 错误使用?

php - 在 codeigniter 的 View 中运行

php - 将数据库中的信息显示到不同单元格中的 HTML 表中

mysql - 选择列出所有事件的最近事件 SQL