Question

I'm creating a login form using CodeIgniter. The login function works perfectly when correct credentials are given. And when wrong credentials are given, it will display a custom message. Now the problem is that if I enter wrong credential once, the url changes to a certain one. Though this displays the same login form, I cannot login even with correct credentials thereafter.

The whole scenario is:

How can I solve this issue?

I have this controller code for login function

<?php
class C_login extends CI_Controller{
function __construct(){
    parent::__construct();
}

public function index($msg = NULL){
    $data['msg'] = $msg;
    $this->load->view('v_login', $data);
}
public function c_fun_login_process(){
    $this->load->model('m_login_model');
    $result = $this->m_login_model->m_fun_login_validate();

    if(! $result){
        $msg = '<font color=red>Invalid username and/or password.</font><br />';
        $this->index($msg);
    }else{

        redirect('c_view_page/c_fun_dashboard');
    }        
}

And this is the login form in the view file (v_login)

  <form method="post" action="<?php '.base_url().'?>c_login/c_fun_login_process" name="process">
             <?php
            if (!is_null($msg))
                echo $msg;
            ?>

This apparently is a minor issue which anyone with a pretty decent knowledge of CodeIgniter can solve. Any help is appreciated.

Was it helpful?

Solution

To be safe, build your form using the native helper:

<?php echo form_open('c_login/c_fun_login_process');?>

Or use any other url function (from the url helper, be sure to have it loaded beforehand):

<form method="post" action="<?php echo site_url('c_login/c_fun_login_process');?>" name="process">

This way you don't have to worry about building wrong urls and facing problems like that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top