How to select authenticated user data with PHP and AngularJS -
i have problem i've been trying solve long time. have read courses php sessions , had proposals use localstorage no avail
the problem:
im working angularjs , php backend, , have 2 views, 1 'login.html' , other 'info.html', 2 controllers (one login function , other selecting user's data) . managed authentication phase second step want when user authenticates, redirected other view (info.html) information of user displayed. how can store data login function , use in second controller(second web service)
login.php
<?php session_start(); $_session['token'] = true; $data = json_decode(file_get_contents("php://input")); $connect = mysqli_connect("localhost", "root", "", "test"); if(count($data) > 0) { $email=mysqli_real_escape_string($connect, $data->email); $mdp=mysqli_real_escape_string($connect, $data->mdp); $query = 'select * `client` (emailclient = "'.$email.'" , mdp= "'.$mdp.'")'; $q = mysqli_query($connect , $query); if(mysqli_num_rows($q) > 0 ) { $token = md5($email.time()."51395+81519851"); $query = "update client set token = '".$token."' emailclient = '".$email."'"; mysqli_query($connect , $query); $_session["logged_in"] = true; $_session["token"] = $token; $_session["email"] = $email; $result['token'] = $token; $resultstring=json_encode($result); $resultstring=str_replace("null", '""', $resultstring); echo $resultstring; exit; } ?>
loginctrl
app.controller('loginctrl', function($scope, $location,$state,$http,$window){ $scope.submit = function() { data = { 'email' : $scope.email, 'password' : $scope.password }; $http.post('http://localhost/deb/login.php', data) .success(function(data, status, headers, config,result) { console.log(data); $state.go('info'); } }) .error(function(data, status, headers, config, result) { console.log('error'); }); } });
infoctrl :
app.controller('infoctrl', function($scope, $http,$state,$filter){ $scope.loadcolis = function(){ $http.get("http://localhost/deb/info.php") .success(function(data){ $scope.names = data; }); }
info.php
<?php session_start(); $connect = mysqli_connect("localhost", "root", "", "test"); $output = array(); $query = "select name,adresse client token = '".$_session['token']."'"; $result = mysqli_query($connect, $query); if(mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_array($result)) { $output[] = $row; } echo json_encode($output); } ?>
i don't know how user authenticated data, how can please?
thanks in advance
in login controller:
app.controller('loginctrl', function($scope, $location,$state,$http,$window, $cookies){ $scope.submit = function() { data = { 'email' : $scope.email, 'password' : $scope.password }; $http.post('http://localhost/deb/login.php', data) .success(function(data, status, headers, config,result) { console.log(data); $cookies.putobject('authenticatedata', data); $state.go('info'); } }) .error(function(data, status, headers, config, result) { console.log('error'); }); } });
in info controller:
app.controller('infoctrl', function($scope, $http,$state,$filter, $cookies){ //use var authenticatedata = cookies.getobject("authenticatedata"); });
this example show how cookies works. can work around in it.
Comments
Post a Comment