2017-09-12 4 views
-1

So habe ich eine einfache Anmeldung entwickelt und Einfügen von Datenseiten mit CodeIgniter, ich möchte zu jedem Benutzer hat ihre eigenen Daten in DB und jeder Benutzer hat ihre eigenen Satz von DB. Mein Fokus hier ist, die Sitzung zuerst zu bekommen, weil ich die Sitzung von meinem Code nicht bekommen kann.PHP Sitzung MVC mit CodeIgniter

Ich bin neu in PHP und benutze nie PHP noch die native zuvor. Hier ist mein Controller:

public function login(){ 
    $this->load->library('session'); 
    $email = $this->input->post('email'); 
    $password = $this->input->post('password');  
    log_message('error','isi email : '.$email); 
    log_message('error','isi password : '.$password); 
    $return = $this->welcome_model->checkLogin($email, $password); 

    if($return){ 
    $this->session->set_userdata('email',$email); 
    $var = $this->session->userdata; 
    redirect('welcome/masuk');} 

Modell:

public function checkLogin($email, $password) 
{ 
    $return = $this->db->get_where('login',array('email'=>$email, 'password' => $password)); 

    if($return->result_array() != NULL){ 

     return TRUE; 

    }else{ 

     return FALSE; 

    } 

} 

Ausblick:

<form action="<?php echo base_url(); ?>welcome/login" method="post"> 
    <input type="email" class="form-control" name="email" placeholder="Email"> 
    <input type="password" class="form-control" name="password" placeholder="Password"> 
    <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button> 
    </form> 

Jede Hilfe ist willkommen! Vielen Dank!

+1

Es kann auf diese Frage keine wirkliche Antwort sein. Sie suchen im Grunde nach einer Codeüberprüfung, und dafür ist Stack Overflow nicht geeignet. –

Antwort

0

In Ordnung, ich glaube, Sie müssen in folgenden Punkten sehen, während machen Login-Seite unter Berücksichtigung -

1. $this->load->library('session'); //Try to put this in autoload.php in application/config. 
    $autoload['libraries'] = array('session'); 

2. <form action="<?php echo base_url(); ?>welcome/login" method="post"> 
    You can replace this in view with 
    <?= form_open() ?>....CODE....<?= form_close() ?> 

3. Try to use form-validation rules something like this 
    $this->form_validation->set_rules(
     'title', 
     'Title', 
     'required|min_length[20]'); 

Und ja für alle Benutzer zu trennen brauchen Sie etwas, das jeder Benutzer trennen, es zu tun auf das sein könnte Basen seiner einzigartigen ID, Benutzername, Benutzergruppe ... etc.

BENUTZERHANDBUCH REFERENZ: https://www.codeigniter.com/user_guide/

, die als pro meine Anwendungsanforderungen

Anmeldungsseite ist
public function login() 
{ 
    $data = $this->data; 
    if(isset($this->session->userdata('user_id'))) 
    { 
     redirect('dashboard'); 
    } 
    $this->form_validation->set_rules(
            'username', 'Username', 
            'required|min_length[5]' 
    ); 
    $this->form_validation->set_rules(
            'password', 
            'Password', 
            'trim|required|min_length[6]' 
    ); 
    if ($this->form_validation->run() == FALSE) 
    { 
     $this->template->set('title', 'Login'); 
     $this->template->load('main', 'contents' , 'login', $data); 
    } 
    else 
    { 
     $username = $this->input->post('username'); 
     $password = $this->input->post('password'); 

     if ($this->main_model->resolve_user_login($username, $password)) 
     { 
      $user_id = $this->main_model->get_user_id_from_username($username); 
      $user = $this->main_model->get_user($user_id);   

      $this->session->user_id  = (int)$user->id; 
      $this->session->username  = (string)$user->username; 
      $this->session->name   = (string)$user->name; 
      $this->session->password  = (string)$user->password; 
      $this->session->created_at = (bool)$user->created_at; 
      $this->session->blog_id  = (bool)$user->blog_id; 
      $this->session->logged_in = (bool)true; 
      redirect('dashboard'); 
     } 
     else 
     { 
      $this->template->set('title', 'Login'); 
      $this->template->load('main', 'contents' , 'login', $data); 
     } 
    } 
} 
+0

Vielen Dank für Ihre Referenz! –