LoginControllerサンプル
<?php
require_once 'BaseController.php';
class Login extends BaseController {
protected $data = array();
public function __construct()
{
parent::__construct();
$this->load->model('admins');
}
public function index()
{
//postデータあり -> バリデーション
if ($this->has_post_data()) {
//バリデーション
$result = $this->admins->validate('login');
//バリデーションOK
if($result){
//メアド、パスワードDB照合
$result = $this->admins->login();
//メアド、パスワードDB照合OK
if($result){
redirect('/');//ログイン済みホーム
return;
}
}
}
$this->view($this->get_template_name(),$this->data);
}
}
AdminsModel
<?php
require_once 'BaseModel.php';
class Admins extends BaseModel {
public $name = 'admins';
public $validation = array(
'login' => array(
array(
'field' => 'email',
'label' => 'メールアドレス',
'rules' => 'trim|xss_clean|required',
'errors' => array(
'required' => '%sは必須です。',
'custom_validation' => '%sかパスワードが異なります。',
),
),
array(
'field' => 'password',
'label' => 'パスワード',
'rules' => 'trim|xss_clean|required',
'errors' => array(
'required' => '%sは必須です。',
),
),
),
);
public function __construct()
{
parent::__construct();
}
public function login($email='',$password='')
{
if(empty($email)){
$email = $this->post('email');
}
if(empty($password)){
$password = $this->post('password');
}
$this->db->where('email',$email);
$result = $this->db->get($this->name);
if($result->num_rows() < 1){
return false;
}
$result_data = $result->result('array');
$data = array_shift($result_data);
$login = password_verify($password,$data['password']);
if($login){
//ログイン成功
$data = array(
"email" => $email,
"is_logged_in" => 1,
"user_id" => $data['id'],
);
$this->session->set_userdata($data);
}
return $login;
}
}