Skip to main content

PHP - Simple User Login Using AngularJS

Screenshot
Body

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. <br />
  2. <?php<br />
  3. $conn=mysqli_connect("localhost", "root", "", "db_angular_login");</p>
  4. <p> if(!$conn){<br />
  5. die("Error: Failed to connect to database!");<br />
  6. }<br />
  7. ?><br />

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. <br />
  2. <!DOCTYPE html><br />
  3. <html lang="en" ng-app = "myModule"><br />
  4. <head><br />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" /><br />
  6. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" /><br />
  7. </head><br />
  8. <body ng-controller="myController"><br />
  9. <nav class="navbar navbar-default"><br />
  10. <div class="container-fluid"><br />
  11. <a class="navbar-brand" href="https://www.sourcecodester.com">Sourcecodester</a><br />
  12. </div><br />
  13. </nav><br />
  14. <div class="col-md-3"></div><br />
  15. <div class="col-md-6 well"><br />
  16. <h3 class ="text-primary">PHP - Simple User Login Using AngularJS</h3><br />
  17. <hr style = "border-top:1px dotted #000;"/><br />
  18. <div class="col-md-3"><br />
  19. <h5>Username:admin</h5><br />
  20. <h5>Password:admin</h5><br />
  21. </div><br />
  22. <div class="col-md-6"><br />
  23. <h3>User Login</h3><br />
  24. <br /><br />
  25. <form ng-submit="userLogin()"><br />
  26. <div class="form-group"><br />
  27. <label>Username</label><br />
  28. <input type="text" class="form-control" ng-model="username" ng-required="true"/><br />
  29. </div><br />
  30. <div class="form-group"><br />
  31. <label>Password</label><br />
  32. <input type="password" class="form-control" ng-model="password" ng-required="true"/><br />
  33. </div><br />
  34. <center><div class="text-danger" ng-model="result">{{result}}</div></center><br />
  35. <br /><br />
  36. <div class="form-group"><br />
  37. <button class="btn btn-primary form-control"><span class="glyphicon glyphicon-log-in"></span> Login</button><br />
  38. </div><br />
  39. </form><br />
  40. </div><br />
  41. </div></p>
  42. <p><script src="js/angular.js"></script><br />
  43. <script src="js/script.js"></script><br />
  44. </body><br />
  45. </html><br />

home.php

  1. <br />
  2. <!DOCTYPE html><br />
  3. <?php<br />
  4. session_start();<br />
  5. if(!ISSET($_SESSION['user_id'])){<br />
  6. header("location:index.php");<br />
  7. }<br />
  8. ?><br />
  9. <html lang="en"><br />
  10. <head><br />
  11. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" /><br />
  12. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" /><br />
  13. </head><br />
  14. <body><br />
  15. <nav class="navbar navbar-default"><br />
  16. <div class="container-fluid"><br />
  17. <a class="navbar-brand" href = "https://www.sourcecodester.com">Sourcecodester</a><br />
  18. </div><br />
  19. </nav><br />
  20. <div class="col-md-3"></div><br />
  21. <div class="col-md-6 well"><br />
  22. <h3 class = "text-primary">PHP - Simple User Login Using AngularJS</h3><br />
  23. <hr style = "border-top:1px dotted #000;"/><br />
  24. <center><h3>WELCOME</h3></center><br />
  25. <a class = "btn btn-danger" href = "logout.php"><span class = "glyphicon glyphicon-log-out"></span> Logout</a><br />
  26. </div><br />
  27. </body><br />
  28. </html><br />

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. <br />
  2. <?php<br />
  3. require_once'conn.php';<br />
  4. $postdata = file_get_contents("php://input");<br />
  5. $json = json_decode($postdata);<br />
  6. $username = $json->username;<br />
  7. $password = $json->password;</p>
  8. <p> $query=mysqli_query($conn, "SELECT * FROM `user` WHERE `username`='$username' && `password`='$password'") or die(mysqli_error());<br />
  9. $row=mysqli_num_rows($query);<br />
  10. $fetch=mysqli_fetch_array($query);<br />
  11. if($row > 0){<br />
  12. session_start();<br />
  13. $_SESSION['user_id'] = $fetch['user_id'];<br />
  14. echo "true";<br />
  15. }else{<br />
  16. echo "false";<br />
  17. }<br />
  18. ?><br />

logout.php

  1. <br />
  2. <?php<br />
  3. session_start();<br />
  4. session_destroy();<br />
  5. header("location:index.php");<br />
  6. ?><br />

script.js
Note: Make sure you save this file inside the js directory in order the script works.

  1. <br />
  2. 'use strict';</p>
  3. <p>var app = angular.module("myModule", [])<br />
  4. .controller("myController", function($scope, $http){<br />
  5. $scope.userLogin = function(){<br />
  6. $scope.result = "";<br />
  7. var request = $http({<br />
  8. method: "POST",<br />
  9. url: "login_query.php",<br />
  10. data: {<br />
  11. username : $scope.username,<br />
  12. password: $scope.password<br />
  13. },<br />
  14. headers: { 'Content-Type': 'application/x-www-form-urlencoded' }<br />
  15. });<br />
  16. request.then(function(response){<br />
  17. if(response.data=="true"){<br />
  18. $scope.username = "";<br />
  19. $scope.password = "";<br />
  20. window.location = "home.php";<br />
  21. }else{<br />
  22. $scope.result="Invalid username or Password";<br />
  23. }<br />
  24. });<br />
  25. };<br />
  26. });<br />

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