Reputation: 385
I've been following this tutorial on simple login using CodeIgniter
http://www.iluv2code.com/login-with-codeigniter-php.html.
Whenever I click the login button, I get redirected to a blank page instead of getting to the "verifylogin" controller. I tried to change form_open('verifylogin') to form action="verifylogin" just to make sure it reaches the verifylogin. It reaches verifylogin but can't seem to be doing the correct functionalities. Why is that so? Why am i redirected to a blank page when the form is submitted? Thank you!
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->load->helper(array('form'));
$this->load->view('login_view');
}
}
?>
View
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple Login with CodeIgniter</title>
</head>
<body>
<h1>Simple Login with CodeIgniter</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('verifylogin'); ?>
<label for="username">Username:</label>
<input type="text" size="20" id="username" name="username"/>
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="passowrd" name="password"/>
<br/>
<input type="submit" value="Login"/>
</form>
</body>
</html>
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class VerifyLogin extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('user','',TRUE);
}
function index() {
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE) {
//Field validation failed. User redirected to login page
$this->load->view('login_view');
} else {
//Go to private area
redirect('home', 'refresh');
}
}
function check_database($password) {
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result) {
$sess_array = array();
foreach($result as $row) {
$sess_array = array(
'id' => $row->id,
'username' => $row->username
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
} else {
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
Upvotes: 2
Views: 2059
Reputation: 2113
sometimes index.php prefix before the your page to avoid this use base_url() function before calling the page in form_open();
like :
echo form_open(base_url()."contact-us");
but before using base_url() make sure that you have called $this->load->helper('url'); in your associate controller.
Upvotes: 2
Reputation: 1189
Did you load the url helper??
$this->load->helper(array('url', 'form'));
check it and let me know please
Upvotes: 0
Reputation: 887
change this:
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
to:
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');
The xss_clean
is probably causing the issue, hope it helps. Also make sure if you have not configured your .htaccess
file to remove index.php
from the url you need to use form_open('index.php/verify_login')
Upvotes: 0