PHP - Simple User Login Using AngularJS

In this tutorial we will create a Simple User Login Using AngularJS. This code will submit the MySQLi POST data to enable the user to login. The code use angular directives to submit the form data and login the data if the user account is valid by sending POST request to the MySQLi server. This a user-friendly program feel free to modify and use it to your system. We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. Lastly, this is the link for the AngularJS https://angularjs.org/.

Creating Database

Open your database web server then create a database name in it db_angular_login, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn=mysqli_connect("localhost", "root", "", "db_angular_login");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as show below. index.php
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app = "myModule">
  3. <head>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  6. </head>
  7. <body ng-controller="myController">
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://www.sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class ="text-primary">PHP - Simple User Login Using AngularJS</h3>
  16. <hr style = "border-top:1px dotted #000;"/>
  17. <div class="col-md-3">
  18. <h5>Username:admin</h5>
  19. <h5>Password:admin</h5>
  20. </div>
  21. <div class="col-md-6">
  22. <h3>User Login</h3>
  23. <br />
  24. <form ng-submit="userLogin()">
  25. <div class="form-group">
  26. <label>Username</label>
  27. <input type="text" class="form-control" ng-model="username" ng-required="true"/>
  28. </div>
  29. <div class="form-group">
  30. <label>Password</label>
  31. <input type="password" class="form-control" ng-model="password" ng-required="true"/>
  32. </div>
  33. <center><div class="text-danger" ng-model="result">{{result}}</div></center>
  34. <br />
  35. <div class="form-group">
  36. <button class="btn btn-primary form-control"><span class="glyphicon glyphicon-log-in"></span> Login</button>
  37. </div>
  38. </form>
  39. </div>
  40. </div>
  41.  
  42. <script src="js/angular.js"></script>
  43. <script src="js/script.js"></script>
  44. </body>
  45. </html>
home.php
  1. <!DOCTYPE html>
  2. <?php
  3. if(!ISSET($_SESSION['user_id'])){
  4. header("location:index.php");
  5. }
  6. ?>
  7. <html lang="en">
  8. <head>
  9. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  10. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  11. </head>
  12. <body>
  13. <nav class="navbar navbar-default">
  14. <div class="container-fluid">
  15. <a class="navbar-brand" href = "https://www.sourcecodester.com">Sourcecodester</a>
  16. </div>
  17. </nav>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6 well">
  20. <h3 class = "text-primary">PHP - Simple User Login Using AngularJS</h3>
  21. <hr style = "border-top:1px dotted #000;"/>
  22. <center><h3>WELCOME</h3></center>
  23. <a class = "btn btn-danger" href = "logout.php"><span class = "glyphicon glyphicon-log-out"></span> Logout</a>
  24. </div>
  25. </body>
  26. </html>

Creating the Main Function

This code contains the main function of the application. This code will login a user account when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below. login_query.php
  1. <?php
  2. require_once'conn.php';
  3. $postdata = file_get_contents("php://input");
  4. $json = json_decode($postdata);
  5. $username = $json->username;
  6. $password = $json->password;
  7.  
  8. $query=mysqli_query($conn, "SELECT * FROM `user` WHERE `username`='$username' && `password`='$password'") or die(mysqli_error());
  9. $row=mysqli_num_rows($query);
  10. $fetch=mysqli_fetch_array($query);
  11. if($row > 0){
  12. $_SESSION['user_id'] = $fetch['user_id'];
  13. echo "true";
  14. }else{
  15. echo "false";
  16. }
  17. ?>
logout.php
  1. <?php
  2. header("location:index.php");
  3. ?>
script.js Note: Make sure you save this file inside the js directory in order the script works.
  1. 'use strict';
  2.  
  3. var app = angular.module("myModule", [])
  4. .controller("myController", function($scope, $http){
  5. $scope.userLogin = function(){
  6. $scope.result = "";
  7. var request = $http({
  8. method: "POST",
  9. url: "login_query.php",
  10. data: {
  11. username : $scope.username,
  12. password: $scope.password
  13. },
  14. headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  15. });
  16. request.then(function(response){
  17. if(response.data=="true"){
  18. $scope.username = "";
  19. $scope.password = "";
  20. window.location = "home.php";
  21. }else{
  22. $scope.result="Invalid username or Password";
  23. }
  24. });
  25. };
  26. });
There you have it we successfully created a Simple User Login using AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment