Handling Login Form Validation
<p>I'm trying to figure out how I should use the login form submission to validate the user login credentials. With this template I have it includes a javascript validation code however I do have my own validation code on the server side I want to use (php). After it goes through the php validation I want it to redirect to either the login form again if there was a problem validating the login OR redirect to the dashboard if there was a successful login.</p>

<p>JS validation:</p>

<pre><code>$(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');
    }
});
});
</code></pre>

<p>Controller:</p>

<pre><code>&lt;?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' =&gt; 'dl', 'theMsg' =&gt; 'This is a Blank Message Box...');

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

    if(!$this-&gt;session-&gt;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-&gt;msgboxes-&gt;buildMsgBoxesOutput(array('display' =&gt; 'show', 'msgs' =&gt;$msgBoxMsgs));
    }
    else
    {
        $msgBoxes = array('display' =&gt; 'none');
    }

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

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

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

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

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

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

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

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

}

/* End of file usermanagement.php */ 
/* Location: ./application/controllers/usermanagement.php */ 
</code></pre>

<p>Any ideas?</p>
