パスワードを使用せずに、ユーザー名のみで認証を Codeigniter に行う方法はありますか?
前もって感謝します!
もちろん、セキュリティが必要ない場合は、単一のユーザー名に基づいてセッションを簡単に設定できます。これは、PHPや他のフレームワーク/言語と同じようにCodeigniterでも可能です。
コントローラの例:
function authenticate()
{
// check postdata
if ($this->input->post('username')):
if (!$this->auth_model->login($this->input->post('username',true))):
$this->session->set_flashdata('message', 'Bad username!');
else:
// go to user page or such...
endif;
endif;
redirect('login'); // take user back to login page
}
上記の例では、データベースでユーザー名をチェックし、ログインセッションを設定するモデル(auth_model)があると想定しています。ユーザーがログインしていない場合は、エラーメッセージが表示され、ログインページに戻されます...
もちろん。ただし、安全なものを保存しようとしていないことを願っています。簡潔な Datamapper ORM ソリューションは、次のようになります。必要に応じて Active Record に適応します。
class Login extends CI_Controller{
function try_username(){
$p = $this->input->post();
if($p){
$u = new User();
$u->where('username', $p['username'])->get();
if($u->exists()){
$data = array("loggedin" => true "username" => $p['username']);
$this->session->set_userdata($data);
redirect('logged_in_controller');
}
}
}
}
<form action='login/try_username' method='post'>
<input type='text' name='username'/>
<input type='submit'/>
</form>
ユーザー名のみが必要です = abc@gmail.com
public function login_ulogin($identity, $remember=FALSE) {
$this->trigger_events('pre_login');
$this->trigger_events('extra_where');
$query = $this->db->select('username,group_id, email, id, password, active, last_login')
->where('username', $identity)
->from('users')
->get();
if ($query->num_rows() == 1)
{
$user = $query->row();
$password = TRUE;//$this->hash_password_db($user->id, $password);
if ($password === TRUE)
{
$this->set_session($user);
$this->update_last_login($user->id);
$this->clear_login_attempts($identity);
if ($remember && $this->config->item('remember_users', 'ion_auth'))
{
$this->remember_user($user->id);
}
$this->trigger_events(array('post_login', 'post_login_successful'));
$this->set_message('login_successful');
return TRUE;
}
}
//Hash something anyway, just to take up time
//$this->hash_password($password);
$this->increase_login_attempts($identity);
$this->trigger_events('post_login_unsuccessful');
$this->set_error('login_unsuccessful');
return FALSE;
}