CI 묻고 답하기

제목 form_validation 질문 드립니다.
글쓴이 꾸숑 작성시각 2013/02/06 06:37:04
댓글 : 4 추천 : 0 스크랩 : 0 조회수 : 19779   RSS
login.php 에서 로그인 할시에  입력한 아이디와 패스워드가  db의 아이디 패스워드와 일치하는지 
검증을 수행하지 못하고 아래 내용이 실행됩니다.
  $data['message'] = '로그인 성공 ' . anchor('account/dashboard', '접속');
뭐가 틀린것인지 질문 드립니다.
참고로 회원 가입시에는 아이디 이메일 중복 첵크가 됩니다.

controllers/account.php

    function login()
    {
        $this->form_validation->set_rules('username', '아이디', 'xss_clean|required');
        $this->form_validation->set_rules('password', '패스워드', 'xss_clean|required|min_length[4]|max_length[12]|sha1|callbackup_password_check');
        $this->username = $this->input->post('username');
        $this->password = sha1($this->_salt . $this->input->post('password'));
        if ($this->form_validation->run() == false)
        {
            $this->load->view('account/login');
        }
        else
        {
            $this->account_model->login();
            $data['message'] = '로그인 성공 ' . anchor('account/dashboard', '접속');
            $this->load->view('account/success', $data);
        }
    }
    function password_check()
    {
        $this->db->where('username', $this->username);
        $query = $this->db->get('users');
        $result = $query->row_array();
        if ($result['password'] == $this->password)
        {
            return true;
        }
        if ($query->num_rows() == 0)
        {
            $this->form_validation->set_message('password_check', 'There was an error!');
            return false;
        }
    }

views/login.php
  <table>
  <?php echo form_open('account/login'); ?>
    <tr>
   <td colspan=2>아이디</td>
 </tr>
   <td><input type="text" name="username" value="<?php echo set_value('username'); ?>" /></td>
   <td><?php echo form_error('username'); ?></td>
    </tr>
    <tr>
   <td colspan=2>패스워드</td>
    </tr>
    <tr>
      <td><p><input type="password" name="password" value="<?php echo set_value('password'); ?>" /></td>
   <td><?php echo form_error('password'); ?></p></td>
    </tr>
    <tr>
      <td colspan=2><input type="submit" name="submit" value="로그인" /></td>
    </tr>
  </table>


태그 패스워드,아이디,로그인,폼검증,form_validation
 다음글 CI썸네일 질문. (3)
 이전글 데이터베이스 입력시 escaping하지 않으려고 합니다... (6)

댓글

한대승(불의회상) / 2013/02/06 08:33:42 / 추천 0
콜백 함수명이 문제네요.

callbackup_password_check


함수명을 요렇게 바꿔보세요.

callback_password_check

$this->form_validation->set_rules('password', '패스워드', 'xss_clean|required|min_length[4]|max_length[12]|sha1|callback_password_check');

변종원(웅파) / 2013/02/06 12:20:09 / 추천 0
sha1도 두번 실행되는것 같은데요?
꾸숑 / 2013/02/06 23:12:55 / 추천 0
불의화상// 말씀하신데로 하니까. .form_validation 제대로 작동 하네요 감사합니다.
웅파 // 덕분에 암호화의 기초지식을 습득하였습니다. 감사합니다.

현재 form_validation통한 폼검증, 입력된정보와 db정보를 비교해서 로그인과 회원가입 정확하게 됩니다.

이제 세션 공부 해야 겠습니다.^^

열공!

꾸숑 / 2013/02/06 23:18:45 / 추천 0

아래는 수정된 내용입니다.  오류가 몇군데 있어서 좀 고생했네요..
아래 링크에서 다운 받으신후에 아래 소스와 비교해 보시면 오류 내용들을 찾을수 있습니다.
출처는 http://www.codeigniter-kr.org/source/view/473/page/2/
저 같은 CI초보자에게 아주 좋은 자료라고 판단됩니다. 오류만 없었다면...


controllers/account.php
<?php

class Account extends Controller
{
    function Account()
    {
        parent::Controller();
        $this->load->library(array('form_validation', 'session'));
        $this->load->helper(array('url', 'form'));
        $this->load->Model('account_model');
        $this->_salt = '12345678987654321';
    }
    function index()
    {
        if ($this->account_model->logged_in() == true)
        {
            $this->dashboard(true);
        }
        else
        {
            $this->load->view('account/details');
        }
    }
    function dashboard($condition = false)
    {
        if ($condition == true or $this->account_model->logged_in() == TRUE)
        {
            $this->load->view('account/dashboard');
        }
        else
        {
            $this->load->view('account/details');
        }
    }
    function login()
    {
        $this->form_validation->set_rules('username', '아이디', 'xss_clean|required');
        $this->form_validation->set_rules('password', '패스워드', 'xss_clean|required|min_length[4]|max_length[12]|callback_password_check');
        $this->username = $this->input->post('username');
        $this->password = sha1($this->_salt . $this->input->post('password'));
        if ($this->form_validation->run() == false)
        {
            $this->load->view('account/login');
        }
        else
        {
            $this->account_model->login();
            $data['message'] = '로그인 성공 ' . anchor('account/dashboard', '접속');
            $this->load->view('account/success', $data);
        }
    }
/*///////////////////////////////////////////////////////////
    function username_check($username)
    {
        $this->db->where('username', $this->username);
        $query = $this->db->get('users');
        $result = $query->row_array();
        if ($result['username'] == $this->username)
        {
            return true;
        }
        if ($query->num_rows() == 0)
        {
            $this->form_validation->set_message('username_check', '존재하지 않는 아이디 입니다.');
            return false;
        }
    }
/////////////////////////////////////////////////////////////
*/
    function password_check($password)
    {
        $this->db->where('username', $this->username);
        $query = $this->db->get('users');
        $result = $query->row_array();
  //echo $result['password'];
  //echo $this->password;
  //exit;
        if ($result['password'] == $this->password)
        {
            return true;
        }
          else
        {
            $this->form_validation->set_message('password_check', '패스워드가 틀렸습니다.');
            return false;
        }
    }
    function register()
    {
        $this->form_validation->set_rules('username', '아이디', 'xss_clean|required|callback_username_exists');
        $this->form_validation->set_rules('email', '이메일', 'xss_clean|required|valid_email|callback_email_exists');
        $this->form_validation->set_rules('password', '패스워드', 'xss_clean|required|min_length[4]|max_length[12]|matches[password_conf]');
        $this->form_validation->set_rules('password_conf', '패스워드(확인)', 'xss_clean|required|matches[password]|sha1');
        if ($this->form_validation->run() == false)
        {
            $this->load->view('account/register');
        }
        else
        {
            $data['username'] = $this->input->post('username');
            $data['email'] = $this->input->post('email');
            $data['password'] = sha1($this->_salt . $this->input->post('password'));
            if ($this->account_model->create($data) === true)
            {
                $data['message'] = "회원가입성공 " . anchor('account/login', 'here') . ".";
                $this->load->view('account/success', $data);
            }
            else
            {
                $data['error'] = "회원가입에 실패했습니다.";
                $this->load->view('account/error', $data);
            }
        }
    }
    function username_exists($username)
    {
        $query = $this->db->get_where('users', array('username' => $username));
        if ($query->num_rows() > 0)
        {
            $this->form_validation->set_message('username_exists', '입력하신 %s 는 존재하는 아이디 입니다. .');
            return false;
        }
        $query->free_result();
        return true;
    }
    function email_exists($email)
    {
        $query = $this->db->get_where('users', array('email' => $email));
        if ($query->num_rows() > 0)
        {
            $this->form_validation->set_message('email_exists', '입력하신 %s 은 존재하는 이메일 입니다.');
            return false;
        }
        $query->free_result();
        return true;
    }
    function logout()
    {
        $this->session->sess_destroy();
        $this->load->view('account/logout');
    }
}

?>