How to use sessions created in php no angularjs (Ionic)?

0

Good evening,

I am creating an app in ionic and angularjs I made login through an ajax request to php which in turn verifies and validates everything through the database, the only problem I am have and how after login is done successfully use the session I create in php.

PHP

<?php

    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST');
    header("Content-type: application/json");
    session_start();

    require_once("../funcoes/funcoes.php");

        $sql = $conexao->prepare("SELECT * FROM users_social WHERE fb_email = :user AND password = :pass ");
        $sql->bindParam(':user', $_GET['email'], PDO::PARAM_STR);
        $sql->bindParam(':pass', sha1($_GET['password']), PDO::PARAM_STR);
        $sql->execute();

        if($sql->rowCount() == 1){

            $row = $sql->fetch(PDO::FETCH_ASSOC);

            $_SESSION = array();
            $_SESSION['user_id'] = $row['id'];
            $_SESSION['nome'] = $row['fb_nome'];
            $_SESSION['user_foto'] = $row['user_foto'];
            $_SESSION['user_slug'] = $row['slug'];

        }else{

            echo "erro ";

        }

?>

Controller

.controller('LoginInterno', function($scope, $http) {
    $scope.Btnlogin= function (input){
        $http.post("https://www.sabeonde.pt/api/api_login.php?email=" + input.email + "&password=" + input.password).success(function (data) {
            window.location = "#/app/home"
            $scope.login = data;
        }).
        error(function (data) {
            alert("Dados Incorrectos");
        });
    };
})
    
asked by anonymous 07.09.2015 / 01:19

1 answer

1

I do this as follows ...

on my page php I save in only session

LOGIN.PHP

<?php

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Content-type: application/json");
session_start();

require_once("../funcoes/funcoes.php");

    $sql = $conexao->prepare("SELECT * FROM users_social WHERE fb_email = :user AND password = :pass ");
    $sql->bindParam(':user', $_GET['email'], PDO::PARAM_STR);
    $sql->bindParam(':pass', sha1($_GET['password']), PDO::PARAM_STR);
    $sql->execute();

    if($sql->rowCount() == 1){

        $row = $sql->fetch(PDO::FETCH_ASSOC);

        $_SESSION["user"] = $row;

    }else{

        echo "erro ";

    }

?>

when I want to access session

Auth.php

session_start();


    if(isset($_SESSION["user"]) && (!empty($_SESSION["user"]))){
      echo json_encode($_SESSION["user"]);
    } else{
      echo json_encode(array("error" => "login"));
    }

 ?>

Controller

$http.get(config.BaseUrl+"/auth.php").success(function(inf){
        if(typeof inf == "object"){
 console.log(inf.id);
 console.log(inf.fb_nome);
 console.log(inf.user_foto);
 console.log(inf.slug);
}
    
07.09.2015 / 06:34