0

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(i.e-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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.