0

In my codeigniter project, when the login button is clicked, it's not redirecting to the user home page. But for my client, it is working well.

When I use the print_r($query) it displays the following error:

CI_DB_mysql_result Object ( [conn_id] => Resource id #32 [result_id] => Resource id #38 > [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 0 [row_data] => )

I have tried result() at the end of my code, but it's still its not working. How do I solve this issue?

My Controller Code is as follows:

<?php 
class Main extends Controller {
   function Main()
   {
        parent::Controller();
   }

function index()
{

    $errMsg     = '' ;
        if($this->input->post('adminUsername') && $this->input->post('adminPassword')){
                    $user_name = $this->input->post('adminUsername');
                    $pass_word = base64_encode($this->input->post('adminPassword'));
                    $query=$this->db->get_where('gm_adminusers',array('adminUsername'=>$user_name,'adminPassword'=>$pass_word,'admin_status'=>'A'));
                    print_r($query);

                    if ($query->num_rows() > 0)
                    {
                            $arrRow =$query->row_array();
                            $newdata = array(
                             'sess_adminId'  =>$arrRow['adminId'],

                       );
                       $this->db_session->set_userdata($newdata);
                        if($this->db_session->userdata('sess_adminId')){
                                    redirect('user/home');
                                }
                                else{
                                    $errMsg ='<span style="color:red">Login Error :Please Check Your input.</span>';    
                                    /*redirect('user/home');    */
                                }   

                    }else{
                    $errMsg ='<span style="color:red">Critical Error:Contact Your Administrator</span>';    


                    }
    }

    $data['errMsg']         = $errMsg;
    $this->load->view('header');
    $this->load->view('index',$data);
    $this->load->view('footer');

}

enter image description here

enter image description here

Tobias Roland
  • 1,182
  • 1
  • 13
  • 35
  • 2
    Your query is returning 0 rows. That's not an "error". P.S. are you sure you typed in the right password? ;) P.P.S. I hope you don't actually think `base64_encode` is secure. – gen_Eric Dec 04 '13 at 19:12
  • 1
    You have no error there. The query doesn't return any result. – Miguel G. Flores Dec 04 '13 at 19:15
  • 1
    FYI `base64_encode` is *NOT* a safe/secure way to hash passwords. CodeIgniter includes libraries to help deal with this, and there are many other libraries out there built for the purpose (bcrypt). Please see http://ellislab.com/codeigniter%20/user-guide/libraries/encryption.html and http://stackoverflow.com/a/7045061/183254 – stormdrain Dec 04 '13 at 19:15
  • 2
    Your SQL query is returning zero rows, to help debug this you can turn on the profiler by including `$this->output->enable_profiler(TRUE);` in your controller so that you can see the queries being run on your database. You can then check to query to debug why it is returning zero rows. – Bad Wolf Dec 04 '13 at 19:17
  • Your "admin_password" is stored as `123456`. I don't think that decodes to 6 characters (which is what you typed in the password box). – gen_Eric Dec 04 '13 at 19:29
  • Hello Friends...... I agree returning 0 is not an error,but the table contains 2 set of records and it is showing 0 in the code. Thats my problem.When i click submit button,the loop executes the query and returns the error message i have provided.. – Nidheesh N Namboodhiri Dec 04 '13 at 19:36
  • if ($query->num_rows() > 0) will not work because 0 rows found and executes the else part and returns the message...My question is why the $query returning 0 records – Nidheesh N Namboodhiri Dec 04 '13 at 19:38
  • @NidheeshNNamboodhiri: Like I said, are you sure you entered the right password? There's no way that the password you entered `base64_encode`s to 123456, the character count is wrong. – gen_Eric Dec 04 '13 at 19:39
  • Okay ..you mean i have to remove this base64_encode code...is it? – Nidheesh N Namboodhiri Dec 04 '13 at 19:39
  • @NidheeshNNamboodhiri: First off, what password are you typing into the page? I'm assuming you are typing in `123456` and expecting it to match, even though you are using `base64_encode`. Second, why do you even have `base64_encode` in the first place, that's *NOT* how you store passwords. Third, it looks like the 2nd password is actually `base64_encoded` and the 1st one isn't. You kinda need to store all the passwords in the same format. – gen_Eric Dec 04 '13 at 19:45
  • So, yes, you should remove it and replace it with a *real* password system. How about: http://www.php.net/manual/en/ref.password.php (if you have PHP < 5.5, try: https://github.com/ircmaxell/password_compat) – gen_Eric Dec 04 '13 at 19:46
  • @RocketHazmat Can you post this as answer ...because now it is working – Nidheesh N Namboodhiri Dec 04 '13 at 19:49

1 Answers1

1

Returning 0 rows is not an "error". It's a valid response to a valid MySQL query. Why is the query returning 0 rows? Because the username and password didn't match.

I assume you are typing 123456 in as your password. No matter how hard you try, when you do base64_encode('123456') you're not gonna get 123456.

What you need to do is remove base64_encode and replace it with a real password solution. PHP 5.5 has one built-in (http://www.php.net/manual/en/ref.password.php)1.

You're gonna need to re-save all your passwords in your database, obviously. Also, make sure to store them all in the same format. You currently have one in plaintext and the other as base64.

1 If you don't have PHP 5.5, you can try this: https://github.com/ircmaxell/password_compat

gen_Eric
  • 223,194
  • 41
  • 299
  • 337