2013-07-17 3 views
0

나는 MVC와 codeigniter를 배우려고하고 있으며 내가 뭘 잘못하고 있는지 알아 내려고하고있다. 이메일과 비밀번호가 포함 된 간단한 양식으로 로그인하려고합니다. 컨트롤러에서 먼저 폼을 포함하는 뷰를로드합니다. 'loginSubmit'버튼에 모든 것이 채워지고 클릭되면 내 컨트롤러에서 'login()'으로 이동해야합니다.Codeigniter - 로그인하지 않음

내 'login()'메서드에서 'validate()'메서드를 호출하는 모델을로드합니다 .e. 뭔가를 반환하면 세션을 시작하고 프로필 페이지로 리디렉션하기 위해 내 컨트롤러에 사실을 반환합니다. 제출되는 누르면 저를 제공 할 때

나는에 로그인에서주는거야 어떤 이제 문제는 항상

그래서 내가

http://localhost/project/index.php/login/ 

로 이동 로그인 메도로 로그인 컨트롤러를 새로 고침 ~

http://localhost/project/index.php/login/login/ 

왜 어떤 일이 일어나고있는 것일까 요?

login_view

<?php 
    $loginEmail = array('placeholder' => "Email", 'name' => "loginEmail"); 
    $loginPassword = array('placeholder' => "Wachtwoord", 'name' => "loginPassword"); 
    $loginSubmit = array('name' => "loginSubmit", 'class' => "btn", 'value' => "Inloggen"); 
    $loginForgot = array('name' => "loginForgot", 'class' => "link", 'value' => "Wachtwoord vergeten?"); 

    echo form_open('login/login', array('class' => 'grid-100 formc')); 
    echo form_input($loginEmail); 
    echo form_password($loginPassword); 
    echo form_submit($loginSubmit); 
    echo form_submit($loginForgot); 
?> 

login_controller

<?php 

    Class Login extends CI_Controller{ 
     public function __construct() {   
      parent::__construct(); 
     } 

     function index(){ 
     $data['content'] = 'login_view'; 
     $this->load->view('templates/template', $data); 
     } 


     function login(){ 
      $this->load->model('login_model'); 
      $query = $this->login_model->validate(); 

      if($query){ 
       $data = array(
        'username' => $this->input->post('loginEmail'), 
        'loggedin' => true 

       ); 

       $this->session->set_userdata($data); 
       redirect('profile/myprofile'); 
      } 
      else{ 
       echo "not logged in"; 
      } 
     } 

    } 

    ?> 

login_model 개월의

<?php 

    Class Login_model extends CI_Model{ 
     function __construct(){ 
      parent::__construct(); 
     } 
     function validate(){ 
      $this->db->where('email', $this->input->post('loginEmail')); 
      $this->db->where('password', md5($this->input->post('loginPassword'))); 
      // I also tried with get_where, but same effect. BTW. what is the difference between where() and get_where() and what is better? 
      //$query = $this->db->get_where('tbl_users', array(('email', $this->input->post('loginEmail'), ('password', $this->input->post('loginPassword'))); 
      $query = $this->db->get('tbl_users'); 

      if($query->num_rows == 1){ 
       return true; 
      } 
     } 

    } 

    ?> 
+0

행이 하나만 있으십니까? "$ query-> num_rows == 1"$ query-> num_rows> 0 "일 경우 어떻게됩니까? – martincpt

+0

나는 db에 2 개의 레코드가 있으며 완전히 다른 레코드가 있습니다. 또한 동일한 전자 메일에 등록 할 수 없으므로 항상 1 또는 0이어야합니다. – mXX

+0

다음에 echo $ this-> input-> post ('loginEmail'). ''. md5 ($ this- > input-> post ('loginPassword'));를 입력하십시오. 그들이 db와 같은지보십시오. – martincpt

답변

1

우리가 생성자가됩니다 같은 클래스 이름을 가진 함수를 만들 때와 같이 (컨트롤러에서) 뭔가 다른 기능 namefrom 로그인을 변경해주십시오. 그래서 로그인 기능이 반복적으로로드됩니다.

+0

항상'else { echo "not logged in"; }'자격 증명이 정확하더라도. – mXX

+0

예, 로그인 기능이 생성자로 실행 중이므로 알고 있습니다. 그래서 function.if가 작동하지 않는다면 코드를 디버그 해보십시오. echo $ query; 주사위(); –

+0

그래, 지금 함수의 이름을'inloggen()'으로 바꿨다. 하지만 이제는 로그인 할 때 기본적으로 자격 증명이 정확하지 않다고 말하는 것이다. – mXX

0

쓰기 다른 부분 델과 form_close() 메소드를 항상 가까운 형태

<?php 

     Class Login_model extends CI_Model{ 

      function validate(){ 
       $this->db->where('email', $this->input->post('loginEmail')); 
       $this->db->where('password', md5($this->input->post('loginPassword'))); 
       // I also tried with get_where, but same effect. BTW. what is the difference between where() and get_where() and what is better? 
       //$query = $this->db->get_where('tbl_users', array(('email', $this->input->post('loginEmail'), ('password', $this->input->post('loginPassword'))); 
       $query = $this->db->get('tbl_users'); 

       if($query->num_rows == 1){ 
       return true; 
       } 
       else{ 
       return false; 
       }  
      }  
     }  
?> 
+0

이것은 작동하지 않았습니다. 여전히 동일한 결과입니다 – mXX

관련 문제