Welcome Guest, Not a member yet? Register   Sign In
[SOLVED]Query problems
#1

[eluser]stuffradio[/eluser]
I'm testing a login library I made. I'm having problems with my login part.

I'm creating test accounts, and I have some usernames that are similar... but are not the same.

I have one called stuff and the other is stuffradio. I used the same password, but that shouldn't matter.

Code:
function login()
   {
     if (!isset($this->username, $this->password))
     {
      return false;
     } else {
       $query = $this->CI->db->get_where('userstable', array('user' => $this->username, 'pass' => md5($this->password)));
       $result = $query->row();        

       if ($query->num_rows() > 0)
       {
          if (strlen($result->confirm) > 0):
          redirect(site_url("url/index/notconfirmed"));
          return false;
          else:
         $this->CI->session->set_userdata(array('log_in' => $result->user));
         redirect(index_page());        
         return $result->user;
         endif;
       } else {
         $status = "Guest";
         $this->CI->session->set_userdata(array('log_in' => $status));
         redirect(site_url('url'));
       }  
     }
   }

When I login with that code, no matter which username I do... it returns stuffradio even though the post says stuff. Should 'stuff' not be returned instead of 'stuffradio'?
#2

[eluser]InsiteFX[/eluser]
Hi,

See the code below, I think it's because your doing the redirect so it never gets to the return.

Code:
function login()
   {
     if (!isset($this->username, $this->password))
     {
      return false;
     } else {
       $query = $this->CI->db->get_where('userstable', array('user' => $this->username, 'pass' => md5($this->password)));
       $result = $query->row();        

       if ($query->num_rows() > 0)
       {
          if (strlen($result->confirm) > 0)
          {
             redirect(site_url("url/index/notconfirmed"));
             return false;
          }
          else
          {
             $this->CI->session->set_userdata(array('log_in' => $result->user));
            
             // NOTE: Your doing a redirect below here, no way will it return $result->user;
             redirect(index_page());        
             return $result->user;
          }
       }
       else
       {
         $status = "Guest";
         $this->CI->session->set_userdata(array('log_in' => $status));
         redirect(site_url('url'));
       }  
     }
   }

Enjoy
InsiteFX
#3

[eluser]stuffradio[/eluser]
No that doesn't fix it, it just breaks and doesn't redirect because something is returning before the redirect.
#4

[eluser]stuffradio[/eluser]
This is a codeigniter issue because the query works fine in phpMyAdmin.
#5

[eluser]InsiteFX[/eluser]
Well if it keeps returning the other name then it must not be setting the session's userdata.

Try doing a var dump or a print_r after you set the session's user4data to see if it is being set.

Enjoy
InsiteFX
#6

[eluser]überfuzz[/eluser]
Code:
function login()
   {
     if (!isset($this->username, $this->password))
     {
      return false;
     } else {
       $query = $this->CI->db->get_where('userstable', array('user' => $this->username, 'pass' => md5($this->password)));
       $result = $query->row();        

       if ($query->num_rows() > 0)
       {
          if (strlen($result->confirm) > 0):
          redirect(site_url("url/index/notconfirmed")); //redirect the user in where login is called. $status = login; if($status){redirect}
          return false;
          else:
         $this->CI->session->set_userdata(array('log_in' => $result->user));
         redirect(index_page());        
         return $result->user;
         endif;
       } else {
         $status = "Guest";
         $this->CI->session->set_userdata(array('log_in' => $status));
         redirect(site_url('url'));
       }  
     }
   }

Look up form_validation and run login() when the form_validation is passed. (leave out the isset() part.) If I were to make this I'd do somethis like this.
Code:
//run query, have a column in the sql-table named access
  if ($query->num_rows() > 0)
       {
          //set session values, like access user-name etc...
          return $status
       } else {
          //set to guest
          return $status
       }



Use the function something like this, quasi-code
Code:
$this->form_validation->set_rules('user', 'username', 'rrequired');

        if ($this->form_validation->run() == FALSE)
        {
            //load the form again (use set_values)
        }
        else
        {
    
            $status = login()
            //load a view and tell the user how it went.
            //or if you like to use redirect
            if($status == 0) {redirect();}// === probably won't work
            else {get to work}                  
        }
#7

[eluser]stuffradio[/eluser]
I just discovered how stupidly stupid I am. I was wondering why my script suddenly doesn't work properly, but it's because the textfield is username and I had it posting user, so it was posting FALSE and it would select the first result it came to for the password.




Theme © iAndrew 2016 - Forum software by © MyBB