まず、私の英語でごめんなさい:D
Codeigniter に簡単にログインしようとしましたが、成功しませんでした。公式ドキュメントを読み、スクリプトを必要最小限に減らしました。ページをリダイレクトまたは更新するたびに、または別のコントローラーに移動すると、セッションが空になります。コントローラーを1つだけ作成し、読み書きを簡単にすると、リフレッシュまたはリダイレクトするまでうまくいきます。
これが私がやりたいことです:1)最初のコントローラーはメインコントローラーです。ログイン フォームを含むビューを読み込みます。このフォームには、validation_form をメソッド化してから username_check コールバックをメソッド化するアクションがあります。
2)ユーザーがログインできた場合、ユーザーデータを設定し、制限されたコントローラーにリダイレクトします。これは私の制限された領域です。
PS i は、オプション データベースをアクティブにして、オートロードでライブラリ セッションを使用します。自動ロードにもデータベースライブラリがあります。
メインコントローラー
class Main extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index()
{
$this->load->view('login_view');
}
public function validation_form()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|callback_username_check');
if ($this->form_validation->run() == FALSE){
echo 'validation_error';
}else{
redirect('/restricted');
}
}
public function username_check(){
$this->load->model('login_model');
$email=$this->input->post('email');
$login=$this->login_model->validate($email);
if ($login != FALSE){
return true;
}
return false;
}
}
ログインモデル
class Login_model extends CI_Model{
public function __construct(){
parent::__construct();
}
public function validate($email){
// Prep the query
$this->db->where('EmailDatore', $email);
// Run the query
$query = $this->db->get('Datore');
// Let's check if there are any results
if($query->num_rows() == 1)
{
// If there is a user, then create session data
$row = $query->row();
$data = array(
'email' => $row->EmailDatore,
'nome' => $row->Nome,
'cognome' => $row->Cognome,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
// If the previous process did not validate
// then return false.
return false;
}
}
RESTRICTED CONTROLLER クラス 制限付き extends CI_Controller{
public function __construct(){
parent::__construct();
$this->check_isvalidated();
}
public function index(){
// If the user is validated, then this function will run
echo 'Congratulations, you are logged in.';
}
private function check_isvalidated(){
if(! $this->session->userdata('validated')){
redirect('main');
}
}
}