0% found this document useful (0 votes)
116 views20 pages

CodeIgniter Database Login Form - Javatpoint

Uploaded by

Rado Rabenitany
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views20 pages

CodeIgniter Database Login Form - Javatpoint

Uploaded by

Rado Rabenitany
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

Home CodeIgniter PHP MySQL Laravel WordPress Magento 2 YII

https://www.javatpoint.com/codeigniter-database-login-form 1/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

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.

In application/controllers

Main.php

In application/views

https://www.javatpoint.com/codeigniter-database-login-form 2/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

login_view.php
invalid.php
data.php
signin.php

In application/models

login_model.php

In the first page, you'll have the option for Login and Sign In.

On Login, if user has entered correct credentials matching to database then he will be directed to
data.php page. But if he has entered wrong information then incorrect username/password message
will appear.

We have named our CodeIgniter folder as login_db. And our table name is signup.

https://www.javatpoint.com/codeigniter-database-login-form 3/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

We need to do some basic settings in our login_db CodeIgniter folder.

Go to autoload.php file, and do the following settings.

In the above snpashot, we have loaded the libraries and helper.

In database.php file, fill your username and database name. Our database name is codeigniter.

https://www.javatpoint.com/codeigniter-database-login-form 4/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

Now, we?ll start the example.

We have made file Main.php in 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()


{

https://www.javatpoint.com/codeigniter-database-login-form 5/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

$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');
}

https://www.javatpoint.com/codeigniter-database-login-form 6/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

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;
}
}

https://www.javatpoint.com/codeigniter-database-login-form 7/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

public function logout()


{
$this->session->sess_destroy();
redirect('Main/login');
}

}
?>

In application/views folder, login_view.php file 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>";

https://www.javatpoint.com/codeigniter-database-login-form 8/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

echo form_close();

?>

<a href='<?php echo base_url()."index.php/Main/signin"; ?>'>Sign In</a>


</body>
</html>

In application/views folder, data.php file 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>

https://www.javatpoint.com/codeigniter-database-login-form 9/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

</body>
</html>

In application/views folder, signin.php file 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();

?>

https://www.javatpoint.com/codeigniter-database-login-form 10/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

</body>
</html>

In application/views folder, invalid.php file 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>

In application/models folder, login_model.php file is made.

<?php

class Login_model extends CI_Model {

public function log_in_correctly() {

$this->db->where('username', $this->input->post('username'));
https://www.javatpoint.com/codeigniter-database-login-form 11/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

$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.

https://www.javatpoint.com/codeigniter-database-login-form 12/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

Enter the information to Login.

After entering information, click on Login button. We have entered the wrong information and hence it
will show the error message as shown below.

https://www.javatpoint.com/codeigniter-database-login-form 13/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

On entering the information from database, we' ll be directed to data.php page.

Click on Login button.

https://www.javatpoint.com/codeigniter-database-login-form 14/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

Clicking on Logout button, you'll be directed to the Main page.

On clicking Sign In, following page will appear.

https://www.javatpoint.com/codeigniter-database-login-form 15/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

https://www.javatpoint.com/codeigniter-database-login-form 16/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

download this example

← Prev Next →

Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

Send your Feedback to [email protected]

https://www.javatpoint.com/codeigniter-database-login-form 17/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

Help Others, Please Share

Learn Latest Tutorials

Splunk tutorial SPSS tutorial Swagger T-SQL tutorial


tutorial
Splunk SPSS Transact-SQL
Swagger

Tumblr tutorial React tutorial Regex tutorial Reinforcement


learning tutorial
Tumblr ReactJS Regex
Reinforcement
Learning

R Programming RxJS tutorial React Native Python Design


tutorial tutorial Patterns
RxJS
R Programming React Native Python Design
Patterns

Python Pillow Python Turtle Keras tutorial


tutorial tutorial
Keras
Python Pillow Python Turtle

Preparation

Aptitude Logical Verbal Ability Interview


Reasoning Questions
Aptitude Verbal Ability
Reasoning Interview Questions

Company
Interview
Questions
Company Questions

https://www.javatpoint.com/codeigniter-database-login-form 18/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

Trending Technologies

Artificial AWS Tutorial Selenium Cloud


Intelligence tutorial Computing
AWS
Artificial Selenium Cloud Computing
Intelligence

Hadoop tutorial ReactJS Data Science Angular 7


Tutorial Tutorial Tutorial
Hadoop
ReactJS Data Science Angular 7

Blockchain Git Tutorial Machine DevOps


Tutorial Learning Tutorial Tutorial
Git
Blockchain Machine Learning DevOps

B.Tech / MCA

DBMS tutorial Data Structures DAA tutorial Operating


tutorial System
DBMS DAA
Data Structures Operating System

Computer Compiler Computer Discrete


Network tutorial Design tutorial Organization and Mathematics
Architecture Tutorial
Computer Network Compiler Design
Computer Discrete
Organization Mathematics

https://www.javatpoint.com/codeigniter-database-login-form 19/20
19/09/2023 15:49 CodeIgniter Database Login Form - javatpoint

Ethical Hacking Computer Software html tutorial


Graphics Tutorial Engineering
Ethical Hacking Web Technology
Computer Graphics Software
Engineering

Cyber Security Automata C Language C++ tutorial


tutorial Tutorial tutorial
C++
Cyber Security Automata C Programming

Java tutorial .Net Python tutorial List of


Framework Programs
Java Python
tutorial
Programs
.Net

Control Data Mining Data


Systems tutorial Tutorial Warehouse
Tutorial
Control System Data Mining
Data Warehouse

https://www.javatpoint.com/codeigniter-database-login-form 20/20

You might also like