Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8438669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:46:45+00:00 2026-06-10T07:46:45+00:00

I am trying to make user authentication with CodeIgniter and jQuery. I have managed

  • 0

I am trying to make user authentication with CodeIgniter and jQuery. I have managed to do the following: If user type wrong user name or password, he remain on the login page and with use of jquery, he get notification that he entered wrong user name or password.

My problem is on success, that is if he type correct user name and password, I manage to send the user on the login page with simple javascript redirect, but that is not what i want. If I use simple redirect with javascript, i can’t fill the user session data, which I need to track the user after the login on all other pages.

I would like if that is possible, if user enter correct user name and password, jquery to do nothing, and function to be activated only in case that user enter wrong user name or password, if that is possible.

Here is the code that I have. JS code first:

 formLogin.on('submit', function(e) {    
    e.preventDefault();
    $.ajax({
    type     : 'POST',
    url      : 'loginCheck',
    data     : formLogin.serialize(),
    dataType : 'json',
    success  : function(r) {


        if (r.status) {
            // this works, but I would like this part to do nothing
            // just to leave my PHP controller to redirect the user
            window.location = 'http://www.linkedin.com';

        } else {
            $('#errorMessageTop').fadeIn();

        }

    }
});

});

And the controller code is:

public function loginCheck()
    { 

       // set the validation rules
        $this->form_validation->set_rules('username', 'Username', 'required|trim|encode_php_tags');
        $this->form_validation->set_rules('password', 'Password', 'required|trim|encode_php_tags');
        $this->form_validation->set_error_delimiters('<br /><p class=jsdiserr>', '</p><br />');
        if ($this->form_validation->run() != FALSE) 
        {

            $ids=array();
            $ids[0]=$this->db->where('username', $this->input->post('username'));
            $ids[1] = $this->db->where('password', md5($this->input->post('password')));
            $query = $this->backOfficeUsersModel->get();

            if($query)
                {
                    $data = array(
                    'username'       => $this->input->post('username'),
                    'isUserLoggedIn' => true
                    ); 
                $isAuthenticated = true;
                if ($isAuthenticated) {
                 $return['status']  = true;
                $return['message'] = 'You have successfully been logged in!'; 
                exit(json_encode($return)); 
                 $this->session->set_userdata($data);
                $data['title'] = "Welcome to dashboard!";
                $data['main_content'] = 'dashboard';
                $this->load->vars($data);
                $this->load->view('backOffice/template');


                }

        } else {   

                $return = array(
                'status'  => false,
                'message' => 'Wrong Username or Password'
                );
                 exit(json_encode($return));

        }
        } else {

             $errorMessage = "Second Message Wrong username or pwd...!";
             $errorMessage = json_encode($errorMessage);

        }



    } // end of function loginCheck 

Any help will be deeply appreciated.

Regards,Zoran

===============

New code from controller:

public function loginCheck()
    { 

       // set the validation rules
        $this->form_validation->set_rules('username', 'Username', 'required|trim|encode_php_tags');
        $this->form_validation->set_rules('password', 'Password', 'required|trim|encode_php_tags');
        $this->form_validation->set_error_delimiters('<br /><p class=jsdiserr>', '</p><br />');
        if ($this->form_validation->run() != FALSE) 
        {

            $ids=array();
            $ids[0]=$this->db->where('username', $this->input->post('username'));
            $ids[1] = $this->db->where('password', md5($this->input->post('password')));
            $query = $this->backOfficeUsersModel->get();

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


                $this->session->set_userdata($data);
                echo json_encode(array("success" => true));
                $data['title'] = "Welcome to dashboard!";
                $data['main_content'] = 'dashboard';
                $this->load->vars($data);
                $this->load->view('backOffice/template');


        } else {   

                echo json_encode(array("success" => false, "error" => "Wrong credentials"));

        }
        } else {

             $errorMessage = "Second Message Wrong username or pwd...!";
             $errorMessage = json_encode($errorMessage);

        }



    } // end of function loginCheck 

And the js code:

  $("#formLogin").submit(function(e){
        e.preventDefault();
        var username = $(this).find("#username").val();
        var password = $(this).find("#password").val();
        var obj = {username: username, password: password};
        var url = $(this).attr("action");
        $.post(url, obj, function(r){
            if(r.success) window.location.replace('http://www.linkedin.com');
            else  $('#errorMessageTop').fadeIn();
        }, 'json');
    })
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:46:46+00:00Added an answer on June 10, 2026 at 7:46 am

    Why are you calling exit()?… That terminates the script and prevents the session from being set. Also, you’re setting $isAuthenticated to true and immediately checking to see if it’s true. Of course it will be.

    This is a stripped-down version of how I handle it… In my Controller:

    function take_creds(){
    
            $p = $this->input->post();
            if($p){
                $u = new User();
                $u->where("username", $p['username']);
                $u->where("password", sha1($p['password']));
                $u->get();
                if($u->exists()){
                    $u->loggedin = 1;
                    $u->save();
    
                    $data = array(
                        'username' => $u->username,
                        'loggedin' => true
                    );
    
                    $this->session->set_userdata($data);
                    echo json_encode(array("success" => true));
                }
                else echo json_encode(array("success" => false, "error" => "Wrong credentials"));   
            }
        }
    

    And the corresponding jQuery:

    $("#login_form").submit(function(e){
                e.preventDefault();
                var username = $(this).find("#login_username").val();
                var password = $(this).find("#login_password").val();
                var obj = {username: username, password: password};
                var url = $(this).attr("action");
                $.post(url, obj, function(r){
                    if(r.success) window.location.replace('home');
                    else console.log("Write this error to the DOM: " + r.error);
                }, 'json');
            })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make an application that needs authentication :- when user type username
I'm trying make a login window where a user is prompted to enter their
I am trying to make my own user control and have almost finished it,
I'm trying to make my custom filter work... I have the following code in
So I am trying to make a link to user's profile! So I have
I'm trying to make a simple username/password authentication in a Spring Security web app.
Here is jsfiddle ! I am trying to make user page where he can
I am trying to make the same user interface to preview image like on
So I'm trying to make a custom user control for a Windows Phone 7
Trying to make an xna game, where the user needs to tap to stop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.