想在 codeigniter 中使用电子邮件和手机登录

Want to login using email and mobile in codeigniter

控制器:

public function index(){
  $this->load->helper(array('form', 'url','common'));
  $this->load->library(array('form_validation','session'));
      $data=array(   
         'header' => $this->load->view('frontend/assets/header', '', TRUE),  
         'footer' => $this->load->view('frontend/assets/footer', '', TRUE),
      );    
      $this->form_validation->set_rules('email', 'email', 'trim|required');
      $this->form_validation->set_rules('password', 'Password', 'trim|required');    
      $this->load->model('usermodel','usermodel',TRUE); 

      $form_post_data = $_POST;
      // print_r($form_post_data);exit;
      $email = $form_post_data['email'];
      $password = $form_post_data['password'];

      // $mobile = $form_post_data['mobile'];

      if(count($form_post_data) >0)
      {      


            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('frontend/index',$data);
            }
            else
            {       

                $user_data  = $this->usermodel->checklogin($email,$password);   
                   var_dump($user_data);exit;
                if(count($user_data) > 0)     
                {
                      $this->session->set_userdata('sess_user_id', $user_data->id);
                      $this->session->set_userdata('sess_user_email', $user_data->email);
                      $this->usermodel->update_last_login_date();
                     echo "<script>
                    alert('Login Successfull.');
                      window.location.href = '" . base_url() . "profile';
                      </script>";
                }
                else 
                {
                  $this->session->set_flashdata('err_msg', 'email or password is invalid please try again');
                  echo "<script>
                  alert('Email and Password is wrong.');
                    window.location.href = '" . base_url() . "home';
                    </script>";
                }
             } 
         }
         else {
            $this->load->view('frontend/user/login',$data);
         }

    }

型号:

 function checklogin($email,$password)
    {

      // unset($data['sbt']);
      $sha_password = md5($password);
      // print_r($email);
      // print_r($sha_password);exit;
      $this->db->select('*');
      $this->db->from('user');
      $this->db->where('email', $email)
      // $this->db->where("status","live")
      ->group_start()
      ->or_where('mobile', $email)
      ->group_end();
      $this->db->where('password', $sha_password);
      $query = $this->db->get(); 
      return $query->row();

    }

查看:

 <form action="<?php echo base_url();?>user" method="POST" autocomplete="off">
                      <div class="field">
                        <input type="text" name="email" id="email" placeholder="E-mail or Phone">
                        <label for="email">E-mail</label>
                      </div>
                      <div class="field">
                        <input type="password" name="password" id="password" placeholder="password">
                        <div toggle="#password-field" class="fa fa-fw fa-eye field-icon toggle-password" id="log_pass" ></div>
                        <label for="password">password</label>
                      </div>

                      <button type="submit" name="sbt" class="modal__form_btn">Login</button>

                      <a href="#" class="modal__forgot_link">Forgot Password ?</a>
                    </form>

想在 codeigniter.I 中使用电子邮件和手机登录,试图在 CI 中使用电子邮件或 phone 创建登录功能。我无法这样做,控制器中使用的 $user_data 正在获取 null 值。这是我担心的主要原因。请帮我做同样的事情。我有 3-4 种类型的 or_where 变体,但其中 none 对我有用。

为什么不尝试直接使用 $_POST 而不是使用变量

$email = $_POST['email'];

或者您可以使用 codeigniter

的输入 class
$email = $this->input->post('email');

你也可以询问 post 变量是否存在,而不是计算 $_POST 变量

if(count($form_post_data) >0)
if(isset($_POST['email']))

试试这个:

$this->db->select('*');
$this->db->from('user');
$this->db->group_start();
$this->db->where('email', $email)->or_where('mobile', $email);
$this->db->group_end();
$this->db->where('password', $sha_password);
$query = $this->db->get();

为了密码安全,请使用河豚算法。 https://www.php.net/password