0

I have been trying to set up an existing CI 3.1 project locally in xampp. The database is connected and the website loads but the login/register/forms are not working.

Upon checking in detail found the forms are not sending in the post data. Post object is empty.

Below is the code.

Login View

<!-- language-all: lang-php-->
<form id="app-form" class="card auth_form" method="post" action="<?php echo site_url('auth/login/') ?>">
 <div class="header">
 <img class="logo" src="<?php echo base_url() ?>assets/images/logo.svg" alt="">
 <h5>Log in</h5>
 </div>
 <div class="body">
 <div class="input-group mb-3">
 <input type="text" class="form-control" placeholder="Email" name="email" value="" required>
 <div class="input-group-append">
 <span class="input-group-text"><i class="zmdi zmdi-account-circle"></i></span>
 </div>
 </div>
 <div class="input-group mb-3">
 <input type="password" class="form-control" placeholder="Password" name="password" value="" required>
 <div class="input-group-append">
 <span class="input-group-text"><a href="<?php echo site_url('/auth/forgotpassword') ?>" class="forgot" title="Forgot Password"><i class="zmdi zmdi-lock"></i></a></span>
 </div>
 </div>
 <!--<div class="checkbox">
 <input id="remember_me" type="checkbox">
 <label for="remember_me">Remember Me</label>
 </div>-->
 <input type="hidden" name="<?=$this->security->get_csrf_token_name()?>" value="<?=$this->security->get_csrf_hash()?>" >
 <button class="btn btn-primary btn-block waves-effect waves-light ladda-button" data-style="expand-right">SIGN IN</button>
 <div class="signin_with mt-3">
 <p class="mb-0"><a href="<?php echo site_url('/auth/signup') ?>" title="Sign Up">Sign Up</a></p>
 <p class="mb-0">or <a href="<?php echo site_url('/auth/forgotpassword') ?>" title="Sign Up">Forgot Password?</a></p> 
 <!--<button class="btn btn-primary btn-icon btn-icon-mini btn-round facebook"><i class="zmdi zmdi-facebook"></i></button>
 <button class="btn btn-primary btn-icon btn-icon-mini btn-round twitter"><i class="zmdi zmdi-twitter"></i></button>
 <button class="btn btn-primary btn-icon btn-icon-mini btn-round google"><i class="zmdi zmdi-google-plus"></i></button>-->
 </div>
 </div>
 </form>

Auth controller

public function login()
{ 
 $response = array('type'=>'', 'page'=>'', 'message'=>'');
 // Load the model
 $this->load->model('AuthModel');
 // Validate the user can login
 $result = $this->AuthModel->validate();
 var_dump($_POST);
 // Now we verify the result
 if(!$result){
 $response['type'] = 'error';
 $response['message'] = 'Invalid username and/or password.';
 }else{
 $response['type'] = 'redirect';
 $response['page'] = site_url().'RecordVerify/lists';
 $response['message'] = 'Login successful';
 }
 echo json_encode($response);
}

Auth Validate

public function validate()
{
 // grab user input
 $email = $this->security->xss_clean($this->input->post('email'));
 $password = $this->security->xss_clean($this->input->post('password'));
 // Prep the query
 $this->db->select("users.*,user_roles.role_name");
 $this->db->from('users');
 $this->db->join('user_roles', 'users.role = user_roles.URID');
 $this->db->where('users.email', $email);
 //$this->db->where('users.user_password', password_verify($password,'user_password'));
 $this->db->where('users.status', 1);
 $query = $this->db->get();
 // Let's check if there are any results
 if ($query->num_rows() != 0) {
 // If there is a user, then create session data
 $row = $query->row();
 $thehashvalue= hash('sha256', $row->salt . hash('sha256', $password) );
 if($row->password == $thehashvalue)
 {
 $data = array(
 'user_id' => $row->id,
 'first_name' => $row->first_name,
 'last_name' => $row->last_name,
 'full_name' => $row->first_name.' '.$row->last_name,
 'email' => $row->email,
 'user_role_id' => $row->role,
 'user_role_name' => $row->role_name,
 'user_report' => $row->report,
 'validated' => true
 );
 $this->session->set_userdata($data);
 return true;
 }else{
 return false;
 }
 } else {
 // If the previous process did not validate
 // then return false.
 return false;
 }
}
asked Mar 30, 2021 at 15:58
3
  • It appears that your Sign up function is an anchor tag so it will only navigate when clicked (without submitting the form data). The SIGN IN button should be sending data. Perhaps try putting var_dump($_POST); die(); at the top of Auth controller to verify that you are receiving the form data? Commented Mar 30, 2021 at 16:12
  • 1
    @dnapierata I have tried debugging and the post returns empty. Commented Mar 30, 2021 at 16:24
  • Check out this issue: stackoverflow.com/questions/35756622/… Commented Mar 30, 2021 at 16:30

1 Answer 1

0

try grab the input first than pass it to model... in your controller Auth/login put this code

$email = $this->security->xss_clean($this->input->post('email')); $password = $this->security->xss_clean($this->input->post('password'));

and than pass it to your model auth/validate by calling

$result = $this->AuthModel->validate($email, $password);

the code should be like this...

AUTH CONTROLLER

public function login()
{ 
 $email = $this->security->xss_clean($this->input->post('email'));
 $password = $this->security->xss_clean($this->input->post('password'));
 $response = array('type'=>'', 'page'=>'', 'message'=>'');
 // Load the model
 $this->load->model('AuthModel');
 // Validate the user can login
 $result = $this->AuthModel->validate($email, $password);
 var_dump($_POST);
 // Now we verify the result
 if(!$result){
 $response['type'] = 'error';
 $response['message'] = 'Invalid username and/or password.';
 }else{
 $response['type'] = 'redirect';
 $response['page'] = site_url().'RecordVerify/lists';
 $response['message'] = 'Login successful';
 }
 echo json_encode($response);
}

AUTH MODEL

public function validate($email, $password)
{
 // Prep the query
 $this->db->select("users.*,user_roles.role_name");
 $this->db->from('users');
 $this->db->join('user_roles', 'users.role = user_roles.URID');
 $this->db->where('users.email', $email);
 //$this->db->where('users.user_password', password_verify($password,'user_password'));
 $this->db->where('users.status', 1);
 $query = $this->db->get();
 // Let's check if there are any results
 if ($query->num_rows() != 0) {
 // If there is a user, then create session data
 $row = $query->row();
 $thehashvalue= hash('sha256', $row->salt . hash('sha256', $password) );
 if($row->password == $thehashvalue)
 {
 $data = array(
 'user_id' => $row->id,
 'first_name' => $row->first_name,
 'last_name' => $row->last_name,
 'full_name' => $row->first_name.' '.$row->last_name,
 'email' => $row->email,
 'user_role_id' => $row->role,
 'user_role_name' => $row->role_name,
 'user_report' => $row->report,
 'validated' => true
 );
 $this->session->set_userdata($data);
 return true;
 }else{
 return false;
 }
 } else {
 // If the previous process did not validate
 // then return false.
 return false;
 }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.