PHP - OOP Login using Angular JS

Getting Started

Please take note that CSS and angular.js used in this tutorial are hosted so you need internet connection for them to work.

Creating our Database

1. Open phpMyAdmin. 2. Click databases, create a database and name it as angular_db. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `user` (
  2.   `userid` INT(11) NOT NULL AUTO_INCREMENT,
  3.   `username` VARCHAR(50) NOT NULL,
  4.   `password` VARCHAR(50) NOT NULL,
  5. PRIMARY KEY(`userid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  7.  
  8. INSERT INTO `user` (`userid`, `username`, `password`) VALUES
  9. (1, 'neovic', 'devierte');
angularlogin Notice that I have inserted one row. You can use this as sample user in our login. Username: neovic Password: devierte

Creating our Connection

Next, we create our connection to our database. This will serve as the bridge between our forms and database. We name this as conn.php.
  1. <?php
  2.  
  3. $conn = new mysqli("localhost", "root", "", "angular_db");
  4.  
  5. if ($conn->connect_error) {
  6.     die("Connection failed: " . $conn->connect_error);
  7. }
  8.  
  9. ?>

index.php

This contains our login form.
  1. <?php
  2.         session_start();
  3.         if(isset($_SESSION['user'])){
  4.                 header('location:home.php');
  5.         }
  6. ?>
  7. <!DOCTYPE html>
  8. <html ng-app="myapp" ng-controller="controller">
  9. <head>
  10.         <title>PHP - OOP Login using Angular JS</title>
  11.         <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  12.         <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  13. </head>
  14. <body>
  15. <div class="container">
  16.         <div class="row">
  17.                 <div class="col-md-6 col-md-offset-3">
  18.                         <center><h2>PHP - OOP Login using Angular JS</h2></center>
  19.                 </div>
  20.         </div>
  21.         <hr>
  22.         <div class="row" id="loginform">
  23.         <div class="col-md-4 col-md-offset-4">
  24.             <div class="login-panel panel panel-primary">
  25.                 <div class="panel-heading">
  26.                     <h3 class="panel-title"><span class="glyphicon glyphicon-lock"></span> Sign In</h3>
  27.                 </div>
  28.                 <div class="panel-body">
  29.                         <form ng-submit="myFunct()">
  30.                         <fieldset>
  31.                                 <div class="form-group">
  32.                                 <input class="form-control" placeholder="Username" name="username" id="username" type="text" ng-model="username" autofocus>
  33.                                 </div>
  34.                                 <div class="form-group">
  35.                                 <input class="form-control" placeholder="Password" name="password" id="password" type="password" ng-model="password">
  36.                                 </div>
  37.                                 <button type="submit" class="btn btn-lg btn-primary btn-block" ng-click="login()"><span class="glyphicon glyphicon-log-in"></span> {{btnName}}</button>
  38.                         </fieldset>
  39.                         </form>
  40.                 </div>
  41.             </div>
  42.         </div>
  43.     </div>
  44.     <div class="row">
  45.         <div class="col-md-4 col-md-offset-4">
  46.                 <div class="alert alert-success"><center>{{alert}}</center></div>
  47.         </div>
  48.     </div>
  49. </div>
  50. <script src="myangular.js"></script>
  51. </body>
  52. </html>

myangular.js

This is our angular js code.
  1. var app = angular.module("myapp", []);
  2. app.controller("controller", function($scope, $http) {
  3.         $scope.btnName = "Login";
  4.         $scope.alert = "Input User Credentials";
  5.         $scope.login = function() {
  6.                 if ($scope.username == null) {
  7.             alert("Please input Username");
  8.         }
  9.         else if ($scope.password == null) {
  10.             alert("Please input Password");
  11.         }  
  12.         else {
  13.                 $scope.btnName = "Logging in...";
  14.                 $scope.alert = "Checking Account. Please Wait...";
  15.             $http.post(
  16.                 "login.php", {
  17.                     'username': $scope.username,
  18.                     'password': $scope.password,
  19.                 }
  20.             ).success(function(data) {
  21.                 if(data!=''){
  22.                         setTimeout(function() {
  23.                                         $scope.alert = "Login Failed. User not Found!";
  24.                                 $scope.btnName = "Login";
  25.                                 $scope.$apply();
  26.                                         }, 3000);
  27.                 }
  28.                 else{
  29.                         setTimeout(function() {
  30.                                 $scope.username = null;
  31.                                 $scope.password = null;
  32.                                 $scope.alert = "Login Successful. Welcome!";
  33.                                 $scope.btnName = "Login";
  34.                                 $scope.$apply();
  35.                         }, 3000);
  36.                         setTimeout(function() {
  37.                                 window.location.reload();
  38.                         }, 4000);
  39.                                        
  40.                 }
  41.             });
  42.         }
  43.         }
  44. });

login.php

This is our PHP login code.
  1. <?php
  2.         include('conn.php');
  3.         session_start();
  4.         $data = json_decode(file_get_contents("php://input"));
  5.         $username = mysqli_real_escape_string($conn, $data->username);
  6.         $password = mysqli_real_escape_string($conn, $data->password);
  7.         $query=$conn->query("select * from user where username='$username' and password='$password'");
  8.         if($query->num_rows > 0){
  9.                 $row=$query->fetch_array();
  10.                 $_SESSION['user']=$row['userid'];
  11.         }
  12.         else{
  13.                 echo "1";
  14.         }
  15.  
  16. ?>

home.php

This is our goto page after a successful login.
  1. <?php
  2.         session_start();
  3.         if(!isset($_SESSION['user'])){
  4.                 header('location:index.php');
  5.         }
  6.         include('conn.php');
  7.         $query=$conn->query("select * from user where userid='".$_SESSION['user']."'");
  8.         $row=$query->fetch_array();
  9. ?>
  10. <!DOCTYPE html>
  11. <html>
  12. <head>
  13.         <title>PHP - OOP Login using Angular JS</title>
  14.         <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  15.         <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  16. </head>
  17. <body>
  18. <div class="container">
  19.         <div class="row">
  20.                 <div class="col-md-6 col-md-offset-3">
  21.                         <center><h2>PHP - OOP Login using Angular JS</h2></center>
  22.                 </div>
  23.         </div>
  24.         <hr>
  25.     <div class="row">
  26.         <div class="col-md-4 col-md-offset-4">
  27.                 <h2><center>Welcome, <?php echo $row['username']; ?></center></h2>
  28.                 <center><a href="logout.php" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Logout</a></center>
  29.         </div>
  30.     </div>
  31. </div>
  32. </body>
  33. </html>

logout.php

Lastly, this is logout to destroy our user session.
  1. <?php
  2.         session_start();
  3.         session_destroy();
  4.         header('location:index.php');
  5. ?>
That ends this tutorial. If you have any comments, question, or suggesstions, feel free to write it below or message me. Happy Coding :)

Add new comment