Welcome Guest, Not a member yet? Register   Sign In
Session starts only at the second login
#1

[eluser]Takapaka[/eluser]
Hi guys,
My problem is with a session that starts only at second attempt of login. Both in IE and Firefox. I tried to search for similar post but could not find it. Anybody had similar problem?

I can ask the guys who are rebuilding my page right now to look for a fix, but they will rip me off again Sad

I am not a programmer myself but at least I think that this is the piece of code that starts the session in my page:

Code:
function trylogin($email, $password, $session = true)
{
    
    log_message('debug', " +trylogin()");
    
    if($email != '' && $password != '')
    {

        // Only continue if user and pass are supplied
        // SHA1 the password if it isn't already
        if(strlen($password) != 40){ $password = sha1($password); }
        
        
        $sql = "SELECT userid FROM emails ".
             "WHERE email ='$email'";
        $query = $this->object->db->query($sql);
        $query = $query->row();
        
        $userid =     $query->userid;
        
            // Check details in DB
        $sql = "SELECT name, surname FROM users ".
             "WHERE userid = $userid ".
             " AND password='$password' LIMIT 1";
        $query = $this->object->db->query($sql);
        // If user/pass is OK then should return 1 row containing username,fullname
        $return = $query->num_rows();

        
        log_message('debug', "++trylogin userid= ".$userid);
        log_message('debug', "++trylogin return= ".$return);
        log_message('debug', "++trylogin email= ".$email);
        log_message('debug', "++trylogin password= ".$password);
        if($return == 1)
        {

          $row = $query->row();
                
          $username = $row->name . ' ' . $row->surname;
          // Set session data array
          $sessdata = array(
                   'userid'   => $userid,              // TODO   fix it
                    'username' =>     $row->name,
                    'loggedin' => TRUE,
                    'fullname' => $username

          );
          if($session == true)
          {
                    // Set the session
                    $this->object->session->set_userdata($sessdata);
                    return TRUE;

          }
          else
          {// param to set the session = false:
            // return the data only without setting session
                    return $sessdata;
          }
        }
        else
        {        // no rows with matching user & pass - ACCESS DENIED!!
                return FALSE;
        }

   }
   else
   {
            // missing username or password
            return FALSE;
    }
}
#2

[eluser]Michael Wales[/eluser]
Holy smokes... what a horrible mess. I just feel sorry for you...

My first guess: Make sure the call to trylogin is passing TRUE as the third parameter (or not passing anything at all, which will default it to true).

Otherwise, ensure the username and password fields are being passed the correct values from your form.
#3

[eluser]Takapaka[/eluser]
Thanks Michael
You worried me even more. Gee, hard to trust anyone these days.

Does CI have a standard function for log in? A standard, tested and working piece of code that there is no need to reinvent the wheel.

I guess this scenario is bloody frequent that one logs with email address and password, isn't it? Even codeigniter.com has it. The difference should be only the call to the database.

Anybody knows the standard?

I would really welcome your help.
Thanks guys
Wojtek
#4

[eluser]Michael Wales[/eluser]
CI doesn't have a standard login library, since every application is different. There are a lot of different libraries available but using one of them may require a major rewrite of your application (or at least the areas that require authentication and use this authentication to display data to the end user).




Theme © iAndrew 2016 - Forum software by © MyBB