Welcome Guest, Not a member yet? Register   Sign In
Login Form. Using a MVC pattern. Help.
#1

[eluser]theif_of_always[/eluser]
Problem: When I input a username or password. Nothing really happens. Or at least I don't think so. I keep getting same non-response or maybe I am just misdirecting it? My code is below. I am not worried about hashing it at the moment, just want to get this to work properly first. Thanks in advance!


MY CONTROLLER:
Code:
public function login() {


   if (isset( $_POST['username'] ) && isset( $_POST['password'] ))
   {

    $this->form_validation->set_rules("password", "password", "trim|required");
      $this->form_validation->set_rules("username", "username", "trim|required");
      

    
    if($this->form_validation->run() == FALSE)
    {
     $this->load->model("LoginModel");
    
    
        $isAuth = $this->LoginModel->login($_POST);
    
    
    
        if($isAuth == TRUE)
       {
      $this->session->set_userdata('user', $_POST['username']);
      redirect("dashboard/login");
      echo 'true';
        }
    
    
    else {
  
    $this->load->view("login_view");
    echo 'yeah';
    }
    
    }
  
   }
  
   else
   {
             $this->load->view("login_view");
    
   }

}
}

MY MODEL

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class LoginModel extends CI_Model {
    
    public function login($_POST)
    {
      
       $this->db->where("username", $_POST['username']);
  $this->db->where("password", $_POST['password']);
      

  $count = $this->db->count_all_results("users");        

  if ($count == 1) {
   $isAuth = TRUE;
  } else {
   $isAuth = FALSE;
  }
  
  return $isAuth;
}

}

MY VIEW

Code:
<?php
$this->load->view("header_view");
?>

      <?php echo validation_errors();?>
      <?php echo form_open('dashboard/login');?>
      
      <label for="username">Username: </label><br />
      &lt;input name='username' id="username" type='text' value='&lt;?php echo set_value('username'); ?&gt;' /&gt;&lt;br />
  
      <label for="password">Password: </label><br />
      &lt;input name='password' id="password" type='text'  value='&lt;?php echo set_value('password'); ?&gt;' /&gt;&lt;br />

      &lt;input type='submit' id="submit" value="Submit" /&gt;
  
   &lt;/form&gt;
  
&lt;?php
$this->load->view("footer_view");
?&gt;
#2

[eluser]Samus[/eluser]
This line:

Code:
if($this->form_validation->run() == FALSE)
change it to true
#3

[eluser]emily12[/eluser]
In the future, consider debugging like echo "works"; or echo "failed";. I like to sometimes dig into the core files and play with them and print arrays from them, which helped tremendously with the form validation (which had a mistake in the original code... i forgot what line though). Switching FALSE to TRUE seems reasonable enough. But if it doesn't work, use echo instead of redirect and see what happens.
#4

[eluser]Samus[/eluser]
[quote author="emily12" date="1336900493"]In the future, consider debugging like echo "works"; or echo "failed";. I like to sometimes dig into the core files and play with them and print arrays from them, which helped tremendously with the form validation (which had a mistake in the original code... i forgot what line though). Switching FALSE to TRUE seems reasonable enough. But if it doesn't work, use echo instead of redirect and see what happens.[/quote]
I've never had a problem with it. And even if you did (which I doubt) it's better to extend the library than edit the actual core.




Theme © iAndrew 2016 - Forum software by © MyBB