CodeIgniter Forums

Full Version: Is there any default return value for function?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am confused with the following coding.
I just read those in a tutorial.

PHP Code:
if ($this->user_model->login() == TRUE) {
 
      redirect($dashboard);


But the login function is actually not returning anything.

PHP Code:
public function login ()
 
   {
 
       $user $this->get_by(array(
 
           'email' => $this->input->post('email'),
 
           'password' => $this->hash($this->input->post('password')),
 
       ), TRUE);

 
       if (count($user)) {
 
           // Log in user
 
           $data = array(
 
               'name' => $user->name,
 
               'email' => $user->email,
 
               'id' => $user->id,
 
               'loggedin' => TRUE,
 
           );
 
           $this->session->set_userdata($data);
 
       }
 
   

So, how does that if condition works here?
It doesn't, the tutorial is wrong.
(03-25-2015, 09:02 AM)Narf Wrote: [ -> ]
PHP Code:
public function login(){

 
       $dashboard 'dashboard';
 
       $this->user_model->loggedin() == FALSE || redirect($dashboard);

 
       $rules $this->user_model->rules;
 
       $this->form_validation->set_rules($rules);

 
       if ($this->form_validation->run() == TRUE) {
 
           // We can login and redirect

 
           if ($this->user_model->login() == TRUE) {
 
               redirect($dashboard);
 
           }
 
           else {
 
               $this->session->set_flashdata('error''That email/password combination does not exist');
 
               redirect('login''refresh');
 
           }
 
       }

 
       $this->load->view('login');
 
   

It doesn't, the tutorial is wrong.
But it works when i enter the right login details.
Throws error for wrong email password combination
If you really look at the code, it only works because someone wrote some bad code, then hacked at it until it produced the result they were looking for.

$this->user_model->login() == TRUE will never be true unless $this->user_model->login() actually returns something. Since it does not, the redirect($dashboard) call inside the if statement is never called.

However, in the else statement, it sets some flashdata and redirects to login (back to the function we're currently evaluating, I would assume, as we already have too many functions named login for my taste). At this point, we have:

PHP Code:
$this->user_model->loggedin() == FALSE || redirect($dashboard); 

Since $this->user_model->loggedin() presumably checks the session's userdata for 'loggedin' == TRUE, it moves on to the portion after the || in this line and redirects to $dashboard, giving the same result that would have occurred if $this->user_model->login() had returned true before you were redirected to the current login method.

I'm not great with sessions, but I'm guessing that redirecting (yet again) is also going to clear out the flashdata that was set in the else condition before the previous redirect, preventing you from seeing the "That email/password combination does not exist" error.

Overall, you should probably find a better tutorial.
(03-25-2015, 10:34 AM)mwhitney Wrote: [ -> ]If you really look at the code, it only works because someone wrote some bad code, then hacked at it until it produced the result they were looking for.

$this->user_model->login() == TRUE will never be true unless $this->user_model->login() actually returns something. Since it does not, the redirect($dashboard) call inside the if statement is never called.

However, in the else statement, it sets some flashdata and redirects to login (back to the function we're currently evaluating, I would assume, as we already have too many functions named login for my taste). At this point, we have:


PHP Code:
$this->user_model->loggedin() == FALSE || redirect($dashboard); 

Since $this->user_model->loggedin() presumably checks the session's userdata for 'loggedin' == TRUE, it moves on to the portion after the || in this line and redirects to $dashboard, giving the same result that would have occurred if $this->user_model->login() had returned true before you were redirected to the current login method.

I'm not great with sessions, but I'm guessing that redirecting (yet again) is also going to clear out the flashdata that was set in the else condition before the previous redirect, preventing you from seeing the "That email/password combination does not exist" error.

Overall, you should probably find a better tutorial.

You are simply awesome! I understood everything clearly now. Thanks a lot!  Smile
Just for now, I modified the code to something like that.
I will write my own code after i completely understand codeigniter!

PHP Code:
$this->user_model->login();
     if(
$this->user_model->loggedin() == TRUE) {
         
redirect($dashboard);
     }