Login page (with database)
In earlier example, we learnt a simple login page with one single username and session.
Now we'll make it with more than one users using database. A sign in and login page will be there for users.
Following pages are made in this example.
Inapplication/controllers
- Main.php
In application/views
- login_view.php
- invalid.php
- data.php
- signin.php
Inapplication/models
- login_model.php
In the first page, you'll have the option forLoginandSign In.
On Login, if user has entered correct credentials matching to database then he will be directed todata.phppage. But if he has entered wrong information then incorrect username/password message will appear.
We have named our CodeIgniter folder aslogin_db.And our table name issignup.
We need to do some basic settings in our login_db CodeIgniter folder.
Go toautoload.phpfile, and do the following settings.
In the above snpashot, we have loaded the libraries and helper.
Indatabase.phpfile, fill your username and database name. Our database name is codeigniter.
Now, we?ll start the example.
We have made fileMain.phpin application/controllers folder,
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
public function index()
{
$this->login();
}
public function login()
{
$this->load->view('login_view');
}
public function signin()
{
$this->load->view('signin');
}
public function data()
{
if ($this->session->userdata('currently_logged_in'))
{
$this->load->view('data');
} else {
redirect('Main/invalid');
}
}
public function invalid()
{
$this->load->view('invalid');
}
public function login_action()
{
$this->load->helper('security');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username:', 'required|trim|xss_clean|callback_validation');
$this->form_validation->set_rules('password', 'Password:', 'required|trim');
if ($this->form_validation->run())
{
$data = array(
'username' => $this->input->post('username'),
'currently_logged_in' => 1
);
$this->session->set_userdata($data);
redirect('Main/data');
}
else {
$this->load->view('login_view');
}
}
public function signin_validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|xss_clean|is_unique[signup.username]');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]');
$this->form_validation->set_message('is_unique', 'username already exists');
if ($this->form_validation->run())
{
echo "Welcome, you are logged in.";
}
else {
$this->load->view('signin');
}
}
public function validation()
{
$this->load->model('login_model');
if ($this->login_model->log_in_correctly())
{
return true;
} else {
$this->form_validation->set_message('validation', 'Incorrect username/password.');
return false;
}
}
public function logout()
{
$this->session->sess_destroy();
redirect('Main/login');
}
}
?>
Inapplication/viewsfolder,login_view.phpfile is made.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Login Page</title>
</head>
<body>
<h1>Login</h1>
<?php
echo form_open('Main/login_action');
echo validation_errors();
echo "<p>Username: ";
echo form_input('username', $this->input->post('username'));
echo "</p>";
echo "<p>Password: ";
echo form_password('password');
echo "</p>";
echo "</p>";
echo form_submit('login_submit', 'Login');
echo "</p>";
echo form_close();
?>
<a href='<?php echo base_url()."index.php/Main/signin"; ?>'>Sign In</a>
</body>
</html>
Inapplication/viewsfolder,data.phpfile is made.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Welcome, You are successfully logged in.</h1>
<?php
echo "<pre>";
echo print_r($this->session->all_userdata());
echo "</pre>";
?>
<a href='<?php echo base_url()."index.php/Main/logout"; ?>'>Logout</a>
</body>
</html>
Inapplication/viewsfolder,signin.phpfile is made.
<!DOCTYPE html>
<html>
<head>
<title>Sign Up Page</title>
</head>
<body>
<h1>Sign In</h1>
<?php
echo form_open('Main/signin_validation');
echo validation_errors();
echo "<p>Username:";
echo form_input('email');
echo "</p>";
echo "<p>Password:";
echo form_password('password');
echo "</p>";
echo "<p>Confirm Password:";
echo form_password('cpassword');
echo "</p>";
echo "<p>";
echo form_submit('signin_submit', 'Sign In');
echo "</p>";
echo form_close();
?>
</body>
</html>
Inapplication/viewsfolder,invalid.phpfile is made.
<!DOCTYPE html>
<html>
<head>
<title>Invalid Page</title>
</head>
<body>
<h1>Sorry, You don't have access to this page.</h1>
<a href='<?php echo base_url()."Main/login"; ?>'>Login Again</a>
</body>
</html>
Inapplication/modelsfolder,login_model.phpfile is made.
<?php
class Login_model extends CI_Model {
public function log_in_correctly() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('signup');
if ($query->num_rows() == 1)
{
return true;
} else {
return false;
}
}
}
?>
On entering the URL, http://localhost/login_db/index.php/Main/
Following page will appear.
Enter the information to Login.
After entering information, click onLoginbutton. We have entered the wrong information and hence it will show the error message as shown below.
On entering the information from database, we' ll be directed todata.phppage.
Click onLoginbutton.
Clicking onLogoutbutton, you'll be directed to the Main page.
On clickingSignIn, following page will appear.