简体   繁体   中英

Get and set session using PHP and angular.js

I need one help session store using PHP and Angular.js . i have one login app.When user will logged in successfully the session will store and when user will redirect to next page the session data will be fetched.I am explaining my code below.

login.php:

<?php 
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$user_name=$request->user_name;
$user_pass=$request->user_pass;
$connect = mysql_connect("localhost", "root", "*****");
mysql_select_db('go_fasto', $connect);
$selquery = "SELECT * FROM db_Admin_Master WHERE user_name='".$user_name."' and password='".$user_pass."'";
$selres = mysql_query($selquery); 
if(mysql_num_rows($selres ) > 0){
    $result=mysql_fetch_array($selres);
    $_SESSION["user_name"]=
    $_SESSION["user_type"]=
    $_SESSION["email_id"]=
    $result['msg'] = 'Login successfull...';
}else{
    header("HTTP/1.0 401 Unauthorized");
    $result['msg'] = 'You entered wrong username/password';
}
echo json_encode($result);
?>

In this page i need to set up the session data( ie-user_name,email_id,user_type ).The user will redirect to the next page after successful login and the controller file of that redirected page is given below.

dashboardController.js:

var dashboard=angular.module('Channabasavashwara');
dashboard.controller('dashboardController',function($scope,$http){
     $http({
         method: 'GET',
         url: 'php/Login/session.php',
         headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
     }).then(function successCallback(response){

     },function errorCallback(response) {

     });
})

In this page the user will get the respective session data inside success function and if session data is not present some message will return to error call back function.Please help me.

I think you need to create one separate function for that. For example

    $selquery = "SELECT * FROM db_Admin_Master WHERE user_name='".$user_name."'    
    and password='".$user_pass."'";
    $selres = mysql_query($selquery); 
    if(mysql_num_rows($selres ) > 0){
        $result=mysql_fetch_array($selres); 
        getSession($result);
    }else{
       header("HTTP/1.0 401 Unauthorized");
        $result['msg'] = 'You entered wrong username/password';
    }
   /*May be in separate function file.*/
   function getSession($result){
       if (! isset ( $_SESSION )) {
            session_start ();
        }
       if( isset($result['user_id'])){ //or Whatever
             // Declare your session and return variable
       } 
   }

And call getSesson() function wherever you need to check session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM