Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我可以将Ion auth设置为通过用户名或电子邮件登录吗_Php_Codeigniter_Codeigniter 2_Ion Auth - Fatal编程技术网

Php 我可以将Ion auth设置为通过用户名或电子邮件登录吗

Php 我可以将Ion auth设置为通过用户名或电子邮件登录吗,php,codeigniter,codeigniter-2,ion-auth,Php,Codeigniter,Codeigniter 2,Ion Auth,我知道我可以在配置中将ion auth设置为通过用户名登录,我也知道我可以在配置中将其设置为通过电子邮件登录 是否有一种简单的方法将其设置为自动使用?如果是自动,您的意思是尝试其中一种,然后再尝试另一种,以查看其中一种是否提供有效的返回: 登录发生在ion_auth_模型行:899中 ->where($this->identity_column, $this->db->escape_str($identity)) 因此,您可以将其更改为“或”,然后尝试两列。你需要在整个

我知道我可以在配置中将ion auth设置为通过用户名登录,我也知道我可以在配置中将其设置为通过电子邮件登录


是否有一种简单的方法将其设置为自动使用?

如果是自动,您的意思是尝试其中一种,然后再尝试另一种,以查看其中一种是否提供有效的返回:

登录发生在ion_auth_模型行:899中

->where($this->identity_column, $this->db->escape_str($identity))

因此,您可以将其更改为“或”,然后尝试两列。你需要在整个模型中做到这一点,因为不仅仅是实际的登录需要考虑,还有一个潜在的问题,即用户有一个电子邮件,它是另一个用户的用户名(无论多么不可能)。

在“身份”ion\u身份验证配置中使用“电子邮件”,然后在ion\u身份验证模型中的$query后添加此代码866

if($query->num_rows() == 0){
    $query = $this->db->select($this->identity_column . ', username, email, id, password, active, last_login')
          ->where('username', $this->db->escape_str($identity))
          ->limit(1)
          ->get($this->tables['users']);
}

我认为更简单的方法是检查$identity变量是否是电子邮件。如果不是电子邮件,则将列设置为“用户名”。 大概是这样的:

$check_column = valid_email($identity) ? $this->identity_column : 'username';   
$query = $this->db->select('username, email, id, password, active, last_login')
    ->where($check_column, $this->db->escape_str($identity))
    ->limit(1)
    ->get($this->tables['users']);
在这种情况下,需要加载电子邮件帮助程序

对我有用

把这个放在你的控制器上

编辑ion_auth_model.php。查找函数login()并更新 使用下面的代码进行编码


我最近分叉了Ion Auth并进行了必要的增强,以便在配置中选择它。叉子位于此处:

我提出了一个请求,希望将其包含在Ion Auth代码库中,但目前还没有被接受。关于它是否会使代码变得复杂,存在一些争论。请给他们留个便条,让他们知道如果这个功能对你有用,你会喜欢它


只需几个代码行即可: 假设这是您的ion_auth配置文件:

$config['identity'] = 'username'; // A database column which is used to login with
您必须运行登录功能两次。如果第一次尝试(使用用户名)未成功,您可以更改标识符并重试:

$this->ion\u auth\u model->identity\u column=“email”


无需对模型、库或自定义查询进行任何必要的修改,无需编辑ion_auth_model,您可以执行以下操作:

$check_column = valid_email($identity) ? $this->identity_column : 'username';   
$query = $this->db->select('username, email, id, password, active, last_login')
    ->where($check_column, $this->db->escape_str($identity))
    ->limit(1)
    ->get($this->tables['users']);
考虑到您已经拥有此配置:

 $config['identity'] = 'username';
您的控制器上有:

// log the user in
public function login()
{
...
    if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
    {
    //if the login is successful
您可以让它进行检查,如果不成功,请将
电子邮件
设置为标识列并进行检查:

// log the user in
public function login()
{
...

    // check for username
    $login = $this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember);
    if(! $login) { // username is not successful
        $this->ion_auth_model->identity_column = 'email';
        // check for email
        $login = $this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember);
    }

    if( $login ) {
         // successful
    } else {
         // both failed
    }
这样做的好处是:由于您没有更改核心文件,因此与任何新的ionAuth更新都更加兼容这样做的缺点是,它必须对数据库进行双重查询


验证控制器代码修改自:

关于ionAuth回购协议的讨论:


    • 您可以在不修改核心代码的情况下执行此操作。如果存在有效的电子邮件,只需动态更改标识列。注:
      ion\u auth\u模型
      ion\u auth

      public function check_login()
      {
          if (!$this->input->is_ajax_request()) {
              exit('No direct script access allowed');
          }
          $this->form_validation->set_rules('username', str_replace(':', '', $this->lang->line('login_identity_label')), 'required');
          $this->form_validation->set_rules('password', str_replace(':', '', $this->lang->line('login_password_label')), 'required');
          if ($this->form_validation->run() == false) {
              $this->form_validation->json_errors();
          }
          $identity = $this->input->post('username');
          if ($this->form_validation->valid_email($identity)) {
              $this->ion_auth_model->identity_column = 'email';
          } else {
              $this->ion_auth_model->identity_column = 'username';
          }
          if ($this->ion_auth->login($identity, $this->input->post('password'), false)) {
              encode_response('success');
          } else {
              encode_response('error', $this->ion_auth->errors());
          }
      }
      

      我希望它可以配置,但谢谢你让我知道。我会在有时间的时候解决这个问题。我很难过他们还没有将它合并到主存储库中。多年来我都没有接触过codeigniter,但假设它能工作,它看起来像一个优雅的解决方案,就像一个救命稻草!
      public function check_login()
      {
          if (!$this->input->is_ajax_request()) {
              exit('No direct script access allowed');
          }
          $this->form_validation->set_rules('username', str_replace(':', '', $this->lang->line('login_identity_label')), 'required');
          $this->form_validation->set_rules('password', str_replace(':', '', $this->lang->line('login_password_label')), 'required');
          if ($this->form_validation->run() == false) {
              $this->form_validation->json_errors();
          }
          $identity = $this->input->post('username');
          if ($this->form_validation->valid_email($identity)) {
              $this->ion_auth_model->identity_column = 'email';
          } else {
              $this->ion_auth_model->identity_column = 'username';
          }
          if ($this->ion_auth->login($identity, $this->input->post('password'), false)) {
              encode_response('success');
          } else {
              encode_response('error', $this->ion_auth->errors());
          }
      }