2011-12-31 3 views
0

사용자 로그인 자격 증명의 유효성을 검사하기 위해 로그인 양식 제출을 사용해야하는 방법을 파악하려고합니다. 이 템플릿을 사용하면 자바 스크립트 유효성 검사 코드가 포함되어 있지만 서버 측에서 (PHP)를 사용하려는 자체 유효성 검사 코드가 있습니다. PHP 유효성 검사가 끝나면 로그인을 확인하는 데 문제가있는 경우 다시 로그인 양식으로 리디렉션하거나 로그인이 성공하면 대시 보드로 리디렉션합니다.로그인 양식 확인 처리

JS 유효성 검증

$(window).load(function(){ 
/* 
* Validate the form when it is submitted 
*/ 
var validatelogin = $("form").validate({ 
    invalidHandler: function(form, validator) { 
     var errors = validator.numberOfInvalids(); 
     if (errors) { 
      var message = errors == 1 
       ? 'You missed 1 field. It has been highlighted.' 
       : 'You missed ' + errors + ' fields. They have been highlighted.'; 
      $('.box .content').removeAlertBoxes(); 
      $('.box .content').alertBox(message, {type: 'warning', icon: true, noMargin: false}); 
      $('.box .content .alert').css({ 
       width: '', 
       margin: '0', 
       borderLeft: 'none', 
       borderRight: 'none', 
       borderRadius: 0 
      }); 
     } else { 
      $('.box .content').removeAlertBoxes(); 
     } 
    }, 
    showErrors : function(errorMap, errorList) { 
      this.defaultShowErrors(); 
      var self = this; 
      $.each(errorList, function() { 
       var $input = $(this.element); 
       var $label = $input.parent().find('label.error').hide(); 
       $label.addClass('red'); 
       $label.css('width', ''); 
       $input.trigger('labeled'); 
       $label.fadeIn(); 
      }); 
    }, 
    submitHandler: function(form) { 
     window.location.replace('dashboard.html'); 
    } 
}); 
}); 

컨트롤러 :

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Usermanagement extends CI_Controller { 

public function __construct() 
{ 
    parent::__construct(); 
} 

public function index() 
{ 
    //Config Defaults Start 
    $msgBoxMsgs = array();//msgType = dl, info, warn, note, msg 
    $cssPageAddons = '';//If you have extra CSS for this view append it here 
    $jsPageAddons = '';//If you have extra JS for this view append it here 
    $metaAddons = '';//Sometimes there is a need for additional Meta Data such in the case of Facebook addon's 
    $siteTitle = '';//alter only if you need something other than the default for this view. 
    //Config Defaults Start 


    //examples of how to use the message box system (css not included). 
    //$msgBoxMsgs[] = array('msgType' => 'dl', 'theMsg' => 'This is a Blank Message Box...'); 

    /**********************************************************Your Coding Logic Here, Start*/ 

    if(!$this->session->userdata('logged_in')) 
    { 
     $bodyContent = "login";//which view file 
    } 
    else 
    { 
     $bodyContent = "cpanel/index";//which view file 
    } 

    $bodyType = "full";//type of template 

    /***********************************************************Your Coding Logic Here, End*/ 

    //Double checks if any default variables have been changed, Start. 
    //If msgBoxMsgs array has anything in it, if so displays it in view, else does nothing.  
    if(count($msgBoxMsgs) !== 0) 
    { 
     $msgBoxes = $this->msgboxes->buildMsgBoxesOutput(array('display' => 'show', 'msgs' =>$msgBoxMsgs)); 
    } 
    else 
    { 
     $msgBoxes = array('display' => 'none'); 
    } 

    if($siteTitle == '') 
    { 
     $siteTitle = $this->metatags->SiteTitle(); //reads 
    } 

    //Double checks if any default variables have been changed, End. 

    $this->data['msgBoxes'] = $msgBoxes; 
    $this->data['cssPageAddons'] = $cssPageAddons;//if there is any additional CSS to add from above Variable this will send it to the view. 
    $this->data['jsPageAddons'] = $jsPageAddons;//if there is any addictional JS to add from the above variable this will send it to the view. 
    $this->data['metaAddons'] = $metaAddons;//if there is any addictional meta data to add from the above variable this will send it to the view. 
    $this->data['pageMetaTags'] = $this->metatags->MetaTags();//defaults can be changed via models/metatags.php 
    $this->data['siteTitle'] = $siteTitle;//defaults can be changed via models/metatags.php 
    $this->data['bodyType'] = $bodyType; 
    $this->data['bodyContent'] = $bodyContent; 
    $this->load->view('usermanagement/index', $this->data); 
} 

function login() 
{ 
    $this->form_validation->set_rules('username', 'Username', 'trim|required|max_length[50]|xss_clean'); 
    $this->form_validation->set_rules('password', 'Password', 'trim|required|max_length[12]|xss_clean'); 

    if ($this->form_validation->run() == FALSE) 
    { 
     $this->index(); 
    } 
    else 
    { 
     $username = $this->input->post('username'); 
     $password = $this->input->post('password'); 

     $user_id = $this->loggedin->check_login($username, $password); 

     if(! $user_id) 
     { 
      redirect('/'); 
     } 
     else 
     { 
      $this->session->set_userdata(array(
       'logged_in' => TRUE, 
       'user_id' => $user_id 
      )); 
      redirect('cpanel/index'); 
     } 
    } 
} 

function logout() 
{ 
    $this->session->sess_destroy(); 
    $this->index(); 
}  

} 

/* End of file usermanagement.php */ 
/* Location: ./application/controllers/usermanagement.php */ 

어떤 아이디어?

+0

귀하의 질문에 정확히 무엇이 있습니까? – Pointy

+0

무엇을해야합니까? js 또는 php를 통해 내 대시 보드로 이동해야합니까? –

+1

피드백을 더 빨리 제공 할 수 있기 때문에 클라이언트 쪽에서 유효성을 검사하는 것이 좋지만 서버 측에서 유효성을 검사하는 것은 필수 항목입니다. 따라서 서버 쪽 유효성 검사 만 수행하거나 두 가지 작업을 모두 수행 할 수 있습니다. 두 규칙 세트가 물론 일치하는지 확인하십시오. – Pointy

답변

1

내가 올바르게 이해하면 로그온 한 사용자의 유효성 검사 방법을 찾는 것이 귀하의 질문이라고 생각합니다. CodeIgniter 컨트롤러 기능에서는 클라이언트 측 (JavaScript)에서이를 이해하기위한 코드로 echo 코드를 검사 할 수 있습니다. 예를 들어 다음과 같이 반환하십시오.

if(valid){ 
    echo "1, You're logged in successfully"; 
} 

그런 다음 JS에서 코드를 분리하고 코드 및 메시지 부분을 사용할 수 있습니다. JS의 사용과 탐색을 위해

:

질문은 CI 기능을 얻을 jQuery를 아약스에서 사용하는 방법과 관련되어있는 경우 window.location.href = 'yourlocation';

, 당신이 단지처럼 사용할 수 있음을 알 수 있습니다 옛날. url: 'yourrootfolder/yourcontroller/yourfunction'

희망 도움말.