Php 处理登录表单验证

Php 处理登录表单验证,php,javascript,Php,Javascript,我正在试图弄清楚应该如何使用登录表单提交来验证用户登录凭据。有了这个模板,它就包含了一个javascript验证代码,但是在我想要使用的服务器端(php)上,我有自己的验证代码。在它通过php验证之后,如果验证登录时出现问题,我希望它再次重定向到登录表单,或者如果成功登录,则重定向到仪表板 JS验证: $(window).load(function(){ /* * Validate the form when it is submitted */ var validatelogin = $(

我正在试图弄清楚应该如何使用登录表单提交来验证用户登录凭据。有了这个模板,它就包含了一个javascript验证代码,但是在我想要使用的服务器端(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 */ 

我想你的问题是,如果我理解正确,如何检测登录用户的验证。在CodeIgniter控制器函数中,您只需检查它,然后用一段代码在客户端(JavaScript)中回送它即可。例如,返回如下内容:

if(valid){
  echo "1, You're logged in successfully"; 
}
然后您可以在JS中拆分它,并使用代码和消息部分

要使用JS进行导航,请使用:

window.location.href='yourlocation'

如果您的问题涉及如何获取CI函数并在jQueryAjax中使用它,我可以告诉您,您可以像以前一样使用它<代码>url:“yourrootfolder/yourcontroller/yourfunction”


希望能有所帮助。

您的问题是什么?我应该做什么?我应该让它通过js或php进入我的仪表板。在客户端进行验证很好,因为您可以更快地提供反馈,但必须在服务器端进行验证。因此,您可以只进行服务器端验证,也可以同时进行这两种验证。当然,要确保这两套规则是一致的。这样我就可以不用js代码的提交处理程序了?是的,除非你想做一些有趣的事情,比如在用户键入时显示“ok/not ok”指示器。但对于我个人来说,登录表单似乎没有必要。对于有效性规则可能不明显的情况,例如新用户创建密码时,它非常有用。