Angular JS Login with Session using PHP/MySQLi Tutorial
Submitted by nurhodelta_17 on Tuesday, January 26, 2021 - 16:17.
This tutorial tackles how to create a login page using Angular JS and set SESSION with PHP/MySQLi. Angular JS is a javascript framework maintained by Google and is capable of creating Single-Page Applications. In this tutorial, we'll set up PHP Session to prevent users from going into our homepage if not authenticated.
Getting Started
I've have used CDN for following :
Boostrap
Angular JS
Angular Route
Since I have used CDN's for the plugins/libraries/frameworks, this means you will be needing an internet connection in order to work the scripts below properly.
Creating our Database
First, we are going to create our database and insert sample users to test our app.
Open phpMyAdmin.
Click databases, create a database and name it as angular.
After creating a database, click the SQL and paste the below codes. See the image below for detailed instruction.
I used this code. I wanted to list my data in another table. For this purpose I added code. But I did not succeed fully.
I added 'controller' and 'service'.
The 'datalist.js' code looks like this (the result is NOT obtained with this code.):
'use strict';
app.controller('datalist', ['$scope', 'listServices', function($scope, listServices){
var listeistek = listServices.listele();
listeistek.then(function(response){
$scope.listem = response.data ;
});
}]);
Alternative codes for 'datalist.js' are (the result is obtained with this code):
'use strict';
app.controller('datalist', ['$scope', 'listServices', function($scope, listServices){
var listeistek = listServices.listele();
listeistek.then(function(response){
$scope.listem = response.data[0] ; //only one record fetch
});
}]);
The 'listServices.js' code looks like this:
'use strict';
app.factory('listServices', function($http, $location, sessionService){
return{
listele: function(){
var datalistem = $http.get('fetchListe.php');
return datalistem;
}
}
});
The 'fetchListe.php' code looks like this:
$row['ID'],
"AH_ID"=>$row['AH_ID'],
"TARIH"=>$row['TARIH'],
"HASTA_ID"=>$row['HASTA_ID'],
"AD"=>$row['AD'],
"SOYAD"=>$row['SOYAD'],
"YAS"=>$row['YAS'],"AHX"=>$row['AHX']);
}
echo json_encode($data);
?>
The result of the 'fetchListe.php' file is: