config.php
file or define dynamically in the controller function. First way of defining is better if configuration is defined on multiple controller functions.
For Example,
[php]
$config['contact_rules'] = array(
'name' => array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required|xss_clean'
),
'email' => array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email'
),
'subject' => array(
'field' => 'subject',
'label' => 'Subject',
'rules' => 'trim|required|xss_clean'
),
'message' => array(
'field' => 'message',
'label' => 'Message',
'rules' => 'trim|required|xss_clean'
)
);
[/php]
In the above code, the Codeigniter forms configuration has two-dimensional key/value pair based array in which the first defines fields and second dimension defines three different property of each item. Like,
field:
name attribute of that field.label:
label text of that field.rules:
denotes the conditions that has to be satisfied.
-> 'required' denotes that form validation class should not be empty.
-> 'valid_email' rule denotes that the inputted email must have correct format.
-> 'trim' rule performs removal operation on the input that was submitted.
-> ‘xss_clean’ rule protects from security threats.CodeIgniter Form Helper
is used instead of writing HTML code.
config.php
file as shown below.
[php]$config['base_url'] = 'http://localhost/login/';[/php] login
with the table <user_login.
[sql]
create database login;
CREATE TABLE IF NOT EXISTS `user_login` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_password` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)
[/sql] user_authentication.php
in the folder application/controllers and enter the below code. When the form is submitted, user existence is also checked in the database.
[php]
<?php
session_start(); //we need to start session in order to access it through CI
Class User_Authentication extends CI_Controller {
public function __construct() {
parent::__construct();
// Load form helper library
$this->load->helper('form');
// Load form validation library
$this->load->library('form_validation');
// Load session library
$this->load->library('session');
// Load database
$this->load->model('login_database');
}
// Show login page
public function index()
{
$this->load->view('login_form');
}
// Show registration page
public function user_registration_show()
{
$this->load->view('registration_form');
}
// Validate and store registration data in database
public function new_user_registration()
{
// Check validation for user input in SignUp form
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('email_value', 'Email', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('registration_form');
}
else
{
$data = array
(
'user_name' => $this->input->post('username'),
'user_email' => $this->input->post('email_value'),
'user_password' => $this->input->post('password')
);
$result = $this->login_database->registration_insert($data);
if ($result == TRUE)
{
$data['message_display'] = 'Registration Successfully !';
$this->load->view('login_form', $data);
}
else
{
$data['message_display'] = 'Username already exist!';
$this->load->view('registration_form', $data);
}
}
}
// Check for user login process
public function user_login_process()
{
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
if(isset($this->session->userdata['logged_in']))
{
$this->load->view('admin_page');
}
else
{
$this->load->view('login_form');
}
}
else
{
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$result = $this->login_database->login($data);
if ($result == TRUE)
{
$username = $this->input->post('username');
$result = $this->login_database->read_user_information($username);
if ($result != false)
{
$session_data = array(
'username' => $result[0]->user_name,
'email' => $result[0]->user_email,
);
// Add user data in session
$this->session->set_userdata('logged_in', $session_data);
$this->load->view('admin_page');
}
}
else
{
$data = array(
'error_message' => 'Invalid Username or Password'
);
$this->load->view('login_form', $data);
}
}
}
// Logout from admin page
public function logout()
{
// Removing session data
$sess_array = array(
'username' => ''
);
$this->session->unset_userdata('logged_in', $sess_array);
$data['message_display'] = 'Successfully Logout';
$this->load->view('login_form', $data);
}
}
?>
[/php]