[英]Logging in with either username or email
我正在写管理员检查,以确保登录到管理员区域的电子邮件是:
到目前为止,我已经编写了以下代码:
控制器:
$this->form_validation->set_rules('userName','userName', 'required|trim|max_length[99]|callback_admin_check|xss_clean');
function _admin_check($adminUsername, $adminEmail)
{
if($this->users_model->admin_check($adminUsername) || $this->users_model->admin_check($adminEmail))
{
$this->form_validation->set_message('admin_check', 'Sorry you have an %s error!');
return FALSE;
}else{
return TRUE;
}
我对此感到困惑的是,如果userGroup == admin
或帐户为userActive == yes
,我希望它接受管理员用户名或电子邮件地址,但是我不确定我将如何构造模型或数据发送到模型。
更新->乔:
乔
只是几个问题:
return false
? 控制器:
function _admin_check($adminUsername = null, $adminEmail = null)
{
$adminUser = $this->user_model->admin_check($adminUsername,$adminEmail);
//if the UN || PW are not correct
if(! $adminUser)
{
$this->session->set_flashdata('login_error', TRUE); // Does not bother adding incorrect data into the session for the Admin Login
$this->form_validation->set_message('admin_check', 'Sorry you have a %s error!');
return FALSE;
}else{
//Set the session data
$this->session->set_userdata('logged_in', TRUE);
$this->session->set_userdata('userId',$adminUser->id);
$this->session->set_userdata('userFirstName',$adminUser->userFirstName);
$this->session->set_userdata('userLastName',$adminUser->userLastName);
$this->session->set_userdata('userEmail',$adminUser->userEmail);
$this->session->set_userdata('userGroup',$adminUser->userGroup);
$this->session->set_userdata('userActive',$adminUser->userActive);
return TRUE;
}
模型:
function admin_check($adminUsername, $adminEmail)
{
if(is_null($adminUsername && is_null($adminEmail)))
{
return FALSE;
}
if(is_null($adminUsername))
{
$login_field = 'userEmail';
$login_name = '$adminEmail';
}else{
$login_field = 'userName';
$login_name = '$adminUsername'
}
$this->db->select($login_field,$login_name);
$this->db->from('users');
$this->db->where('userName', $adminUsername , 'userEmail' , $adminEmail );
$query = $this->db-get();
if($query->num_rows() > 0)
{
return TRUE;
}else{
return FALSE;
}
}
您可以创建模型的方法以接收2个参数,即用户名和电子邮件。
function admin_check($username = NULL, $email = NULL)
现在,如果要使用用户名进行检查,只需调用admin_check($username)
类的函数admin_check($username)
。 但是,如果要通过电子邮件进行检查,则可以将NULL
作为第一个参数传递,将电子邮件作为第二个参数传递。
然后在函数内部,创建一个if
语句以检查第一个参数是否为NULL
。 如果是这样,请使用电子邮件地址进行检查。 否则,请使用用户名进行检查。
if ($username === NULL)
{
// use the email address to do the authentication
}
else
{
// use $username instead
}
将两个参数的默认值都设置为null: function _admin_check($adminUsername = null, $adminEmail = null)
然后在模型中方法的顶部添加以下内容:
function admin_check($adminUsername = null, $adminEmail = null)
{
if (is_null($adminUsername) && is_null($adminEmail))
{
return false;
}
if (is_null($adminUsername))
{
$login_field = 'email'; // field in the table
$login_name = $adminEmail; // value of the field
}
else
{
$login_field = 'username';
$login_name = $adminUsername;
}
// Your SQL here, using $login_field and $login_name
要使用用户名致电,请按以下方式致电:
$this->users_model->admin_check($adminUsername);
$this->users_model->admin_check($adminUsername, null); // or this
对于电子邮件
$this->users_model->admin_check(null, $adminEmail);
称之为:
$this->users_model->admin_check($adminUsername, $adminEmail);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.