Sunday 15 May 2016

PHP Login Script using OOP


Hi friends in this blog we will learn how to develop PHP Login script by using PHP Object Oriented Programming concept. If you are good php programmer then you definitely follows object oriented programming principals. It is mostly work with objects and easy to maintain your code. In this blog I like to describe how to create user login system by implementing PHP Object Oriented Programming. Here First I have create simple databases class and make database connection, I have write database connection code in construct() megic method of PHP OOP, this is because whenever new object of  this created this code will execute. After this I have make two function one for required field validation and other function will check the user enter correct information like username or password. This is my simple description, if you want to get more information, you can see the video of this post.


Source Code

database.php


 <?php   
 //database.php  
 class Databases{  
      public $con;  
      public $error;  
      public function __construct()  
      {  
           $this->con = mysqli_connect("localhost", "root", "", "testing");  
           if(!$this->con)  
           {  
                echo 'Database Connection Error ' . mysqli_connect_error($this->con);  
           }  
      }  
      public function required_validation($field)  
      {  
           $count = 0;  
           foreach($field as $key => $value)  
           {  
                if(empty($value))  
                {  
                     $count++;  
                     $this->error .= "<p>" . $key . " is required</p>";  
                }  
           }  
           if($count == 0)  
           {  
                return true;  
           }  
      }  
      public function can_login($table_name, $where_condition)  
      {  
           $condition = '';  
           foreach($where_condition as $key => $value)  
           {  
                $condition .= $key . " = '".$value."' AND ";  
           }  
           $condition = substr($condition, 0, -5);  
           /*This code will convert array to string like this-  
           input - array(  
                'id'     =>     '5'  
           )  
           output = id = '5'*/  
           $query = "SELECT * FROM ".$table_name." WHERE " . $condition;  
           $result = mysqli_query($this->con, $query);  
           if(mysqli_num_rows($result))  
           {  
                return true;  
           }  
           else  
           {  
                $this->error = "Wrong Data";  
           }  
      }       
 }  
 ?>  

can_login.php


 <?php  
 include 'database.php';  
 session_start();  
 $data = new Databases;  
 $message = '';  
 if(isset($_POST["login"]))  
 {  
      $field = array(  
           'username'     =>     $_POST["username"],  
           'password'     =>     $_POST["password"]  
      );  
      if($data->required_validation($field))  
      {  
           if($data->can_login("users", $field))  
           {  
                $_SESSION["username"] = $_POST["username"];  
                header("location:login_success.php");  
           }  
           else  
           {  
                $message = $data->error;  
           }  
      }  
      else  
      {  
           $message = $data->error;  
      }  
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial | Login Form in PHP using OOP</title>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
      </head>  
      <body>  
           <br />  
           <div class="container" style="width:500px;">  
                <h3 align="">Login Form in PHP using OOP</h3><br />  
                <?php  
                if(isset($message))  
                {  
                     echo '<label class="text-danger">'.$message.'</label>';  
                }  
                ?>  
                <form method="post">  
                     <label>Username</label>  
                     <input type="text" name="username" class="form-control" />  
                     <br />  
                     <label>Password</label>  
                     <input type="password" name="password" class="form-control" />  
                     <br />  
                     <input type="submit" name="login" class="btn btn-info" value="Login" />  
                </form>  
           </div>  
           <br />  
      </body>  
 </html>  

14 comments:

  1. Really the easiest ways to understand php your professional and genuine

    ReplyDelete
  2. Thanks and Impressive Article. Our offshore Development Center in India that caters clients across the globe with its professional PHP development services. Our professional, experienced dedicated PHP development team ensures complete transparency and work in line with client’s staff. The team will coordinate and adhere with the client’s requirements and time schedules to ensure timely completion of the process.

    ReplyDelete
  3. Very useful information. Keep sharing this type of post. I am really thankful for this post. best php training in pune

    ReplyDelete
  4. seriously, thanks 4 this useful info... u just help 4 my project TT

    ReplyDelete
  5. You are like a php manual book. your articles are good, keep it up. thanks

    ReplyDelete
  6. Where do you get $table_name value from?

    ReplyDelete
  7. haaa haa Hi friends in this blog we will learn how to develop PHP Login script by using PHP Object Oriented

    ReplyDelete
  8. You are Great awsome i like Your Code very Much
    i also view your Laravel lecture consist on 25 series please make More on Laravel

    ReplyDelete
  9. How to login attempt user or forgot password
    ?

    ReplyDelete
  10. Fatal error: Uncaught TypeError: mysqli_num_rows(): Argument #1 ($result) must be of type mysqli_result, bool given..
    please solve this

    ReplyDelete
  11. Your way of putting up code is wonderful, I like it very much

    ReplyDelete