I have a login form that is using CI form validation.
The form is at domain.com/login
It runs a function called validate_credentials and if the validation fails it reloads the view. The problem is the URL continues to show:
domain.com/login/validate_credentials
How can I remove the /validate_credentials?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_error_delimiters('<div class="alert alert-error">', '</div><br />');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login/login');
}
else
{
$this->load->view('clock/clock');
}
}
function validate_credentials() {
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) {// staff credentials validated...
$data = array(
'email' => $this->input->post('email'),
'staff_logged_in' => TRUE
);
$this->session->set_userdata($data);
redirect('clock/clock');
}
else { // not validated reload index function
$this->index();
}
}
}
Instead of calling the other controller function, do a redirection to the index. Because functions in controllers in CodeIgniter are setting the URL scheme, the way you do it right now is just executing the index function, but within the validate_credentials function (and URL).
Do like this:
If you need to display a error or success message, please refer to flash_message of the session library of CodeIgniter, but here is an example.
Set the error message in your controller, juste before redirecting to the index:
And in your index view, you display it if it exists:
Please note that a flash_message is shown once. Which means your index will show your error and then destroy the session message. This is the purpose of flash_message, to correctly and simply show error messages.