C

Simplified tutorials to learn the popular basic language C

VB.Net

Learn the basic desktop development application language

CodeIgniter

One of the popular PHP three tier (MVC) framework

User Login Validation with Email and Password


Here let us create a login form validation with user email address and password. In previous article, we created a registration form with user's details such as name, address, email id, password, etc. So this article is based on previous registration, that is, a user can login to his account with his email and password.



Controller function ‘userlogin’ calls the login form view page ‘loginpage.php’

Controller: (user)

public function userlogin()
  {
   $this->load->view('user/loginpage');
  }

View : user/loginpage.php

<html>
<head>
<title>User Login</title>
</head>
<body bgcolor="#b1ac4e">
<center>
<div class="maindiv">
<h3>User Login</h3>
<form action="logincheck" method="post">
<table>
 <tr>
    <td> <b>Email  </b> </td><td><input type="email" name="usermail"/></td>
 </tr>
 <tr>
   <td> <b>Password  </b> </td><td><input type="password" name="userpwd"/></td>
 </tr>
 <tr>
    <td> <b> </b> </td>
    <td>
    <input type="submit" value="Login" style="height: 25px; width: 100px; float: right;"/>
    </td>
   </tr>
</table>
</form>
<div>
</center>
</body>
</html>

<style>
.maindiv
{
  margin-top: 15%;
  background: gray;
  padding: 15px;
  width: 275px;
  height: auto;
  border: 5px solid black;
}
</style>

User Login form in CodeIgniter

While user login with email and password, submit button action calls a controller function ‘logincheck’.

Controller (logincheck):

public function logincheck()
  {
    $email = $this->input->post("usermail");
    $password = $this->input->post("userpwd");
    $this->load->Model('ProjectModel');
    $result = $this->ProjectModel->checkuser($email,$password);

    if ($result == false)
    {
      echo "Invalide Email or Password";
    }
    else
     {
      echo "Welcome ".$result;
     }
  }

Explanation:

Here function ‘logincheck’ gets the value of user email and password. Controller calls a model function ‘checkuser’ with parameters of user email and password.

Model (checkuser):

public function checkuser($email,$password)
{
  $this->load->database();
  $query = $this->db->query("SELECT user_name FROM table_user WHERE user_email= '$email' AND user_pwd = '$password'");
     if ($query->num_rows() > 0)  
      {
        $row = $query->row();
        return $row->user_name;
       }
     else
       {
         return false;
        }
}

Explanation:
  •   $query = $this->db->query("SELECT user_name FROM table_user WHERE user_email= '$email' AND user_pwd = '$password'");
This database query retrieves a user name, where database email id and password that match entered email and password.
  • $query->num_rows() > 0
This line of code counts the result of the query. If there is a such correct username and password on database, then count shout be 1, that is greater than 0.  Then Model function return back the required user name into the controller function. If the count is zero, then model return ‘false’.

In Controller,

if ($result == false)
    {
      echo "Invalide Email or Password";
    }
Here controller verify the return value that is stored on the variable ‘$result’. If it is false, that means, the entered email of password is wrong, then a invalid message is printed using echo.

Invalid user login in codeIgniter

else
     {
      echo "Welcome ".$result;
     }

If it is not ‘false’, then the result contains a database retrieved name of the user.

User login successfull login message

The retrieved user name is concatenated with a text 'Welcome'.

No comments:

Post a Comment