简体   繁体   English

如何从PHP进入AngularJS进行会话重定向

[英]How to get session from PHP into AngularJS for redirect

I try to made a login system but I want the session can be taken from PHP code. 我尝试制作一个登录系统,但我希望可以从PHP代码中获取会话。 If session login is false or null it will redirect to login page. 如果会话登录为falsenull ,它将重定向到登录页面。 Here my run AngularJS code : 这是我运行的AngularJS代码:

app.run(["$rootScope", 'adminService', '$location', function($rootScope, adminService, $location) {
    $rootScope.$on("$routeChangeStart", function() {
        $location.path('/login');
    });
}]);

Routing code : 路由代码:

app.config(function($routeProvider){
    $routeProvider.when('/', {
        templateUrl: 'welcome.html'
    }).when('/administrator', {
        templateUrl: './part/administrator.html',
        controller: 'adminPageControl'
    }).when('/administrator/:id_admin', {
        templateUrl: './part/addit-administrator.html',
    }).when('/alternatif', {
        templateUrl: './part/alternatif.html',
        controller: 'alternatifPageControl'
    }).when('/alternatif/:id_alternatif', {
        templateUrl: './part/addit-alternatif.html'
    }).when('/skala', {
        templateUrl: './part/skala.html',
        controller: 'skalaPageControl'
    }).when('/skala/:id_skala', {
        templateUrl: './part/addit-skala.html'
    }).when('/kriteria', {
        templateUrl: './part/kriteria.html',
        controller: 'kriteriaPageControl'
    }).when('/kriteria/:id_kriteria', {
        templateUrl: './part/addit-kriteria.html'
    }).when('/klasifikasi', {
        templateUrl: './part/klasifikasi.html',
        controller: 'klasifikasiPageControl'
    }).when('/klasifikasi/:id_klasifikasi', {
        templateUrl: './part/addit-klasifikasi.html'
    }).when('/analisis', {
        templateUrl: './part/topsis.php',
        controller: 'analisisPageControl'
    }).when('/login', {
        templateUrl: './login.html'
    }).when('/logout', {
        template: 'logout'
    }).otherwise({
        redirectTo: '/'
    });
});

PHP function session code : PHP函数会话代码:

function logincheck(){
    return $_SESSION['authid'];
}

logincheck();

You should create an unique session token in your backend without parsing the PHP-Session states directly into your client. 您应该在后端中创建唯一的会话令牌,而无需将PHP-Session状态直接解析到客户端中。 Remember, AngularJS is made for SPA. 记住,AngularJS是为SPA设计的。 So make your client communicate with backend by using an API. 因此,请使用API​​使您的客户端与后端通信。 For example a RESTful API . 例如RESTful API Once the user logged in via the client, return a unique session token to the client. 用户通过客户端登录后,请向客户端返回唯一的会话令牌。 Put and validate this token on every secured data HTTP-Request. 在每个受保护的数据HTTP请求上放置并验证此令牌。 Take a look at oAuth handling , its the same. 看一下oAuth处理 ,同样。

You could create a token simply like: $token = md5('what-ever-you-want'); 您可以简单地创建令牌: $token = md5('what-ever-you-want'); and implement it into your API logic. 并将其实施到您的API逻辑中。 Here you will find more information about handling security interfaces . 在这里,您将找到有关处理安全接口的更多信息。

在此处输入图片说明

The other answer is right. 另一个答案是正确的。 It's a good practice to communicate with backend by using an API, because AngularJS is made for SPA. 通过API与后端通信是一个好习惯,因为AngularJS是为SPA设计的。 However if you insist on using session, it's ok. 但是,如果您坚持使用会话,那就可以了。 You can create a router such as /api/logincheck 您可以创建一个路由器,例如/api/logincheck

PHP: PHP:

public function index()
{
    $res = ['login'=>false];
    if(isset($_SESSION['authid']))
    {
        $res['login'] = true;
    }
    echo json_encode($res);
}

JS: JS:

$http.get('http://XX/api/checklogin').success(function(res){
    if(res.login)
    {
        #handle it
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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