Developing a user login in CodeIgniter

How to make a user login system, access by username and password, in a PHP web page developed with the CodeIgniter framework.

This is a practical article that can help us learn some development techniques in the CodeIgniter framework, as well as give us a useful guide for creating a system that allows users who visit our web application to log in.

The login system is one of the first problems that we will want to solve in web applications. It is nothing more than a module that identifies users who visit a web page, to implement a level of security through a username and password. Of course, in CodeIgniter applications, one of our first steps will be to develop that authenticated access. Therefore, we will get down to work to explain step by step a possible implementation of a script to facilitate user authentication on our website.

Note: The login system has been discussed previously in .com. Of course, when we are developing in CodeIgniter, the ways in which we have to deal with problems differ a bit from traditional PHP programming (without using any framework). So, we pass you a couple of useful references to complement this information.

Development of a

5 easy steps to create a login system in CodeIgniter

We begin by listing the steps we have taken to build this user authentication system.

  • Step 1 – Create the php.php file and save it to /application/controllers/
  • Step 2 – Create the login.php file and save it to /application/views/
  • Step 3: Create users_model.php and save it to /application/models/
  • Step 4 – Create the mysql table with some test users
  • Step 5: Important: Automatically pre-load CodeIgniter libraries for database and form validations
See also  Web design for mobile devices

Next, you can see the code lists of each of the scripts that we have used. Later in this article, after the listings, you will be able to read a description of each of these implementation steps.

Step 1: Code for the “php.php” controller:

class Php extends CI_Controller {

function login($language=null)
{

// $this->config->set_item(‘language’, ‘english’); // Dynamically set the language we want our application to run
if(!isset($_POST)){ // If we don’t get any value from the form, it means the user just logged in.
$this->load->view(‘login’); // Therefore we present you with the input form screen.
}
else{ // If the user has already gone through the initial screen and pressed the “Enter” button
$this->form_validation->set_rules(‘maillogin’,’e-mail’,’required|valid_email’); // We configure the validations using the form_validation library of the Codeigniter Framework
$this->form_validation->set_rules(‘passwordlogin’,’password’,’required’);
if(($this->form_validation->run()==FALSE)){ // Check if the user passed the validation
$this->load->view(‘login’); // If not, we present the login screen again
}
else{ // If both fields were correctly filled in by the user,
$this->load->model(‘users_model’);
$ExistsUserandPassoword=$this->users_model->ValidateUser($_POST,$_POST); // check that the user exists in the database and the password entered is correct
if($ExisteUsuarioyPassoword){ // The variable $ExisteUsuarioyPassoword receives the value TRUE if the user exists and FALSE if not. This value is determined by the model.
echo “Validation Ok

Back“; // If the user entered valid access data, we print a successful validation message on the screen
}
else{ // If it failed to validate
$data=”Incorrect email or password, please try again”;
$this->load->view(‘login’,$data); // We return it to the login screen and pass as a parameter the error message to be displayed on the screen
}
}
}
}
}
?>

See also  How to know if a field is NULL in a SQL query with MySQL

Step 2: Code for the “login.php” view:




Script demo: How to do a user login in php?










E-mail:

if(isset($error)){
echo “

“.$error.”

“;
}
echo form_error(‘maillogin’);
?>

Password:

Access data (demo)

(corresponding to a user already entered in the database)

email: diego@blogdephp.com

password: blogdephp

LinkedInShare

Loading Facebook Comments ...
Loading Disqus Comments ...