jquery - How to check whether session exists from javascript? - Stack Overflow

I have an app with frontend in javascript (using AngularJS and jQuery) and backend in java. The app has

I have an app with frontend in javascript (using AngularJS and jQuery) and backend in java. The app has many pages (ng-view).

I need to know whether the session in the backend is currently active or invalidated. Based on that I need to decide whether to show Logout button on the page or not.

Although I have learnt that the session is maintained in the browser using session cookies, from this and this, I concluded that we have to manually set the variable values in the cookie object that we create in javascript.

Is there any way to get the session cookie that is used to municate with server? By getting that cookie, we can perhaps see whether the session is currently active or not.

Please let me know if my interpratation is wrong.

I have an app with frontend in javascript (using AngularJS and jQuery) and backend in java. The app has many pages (ng-view).

I need to know whether the session in the backend is currently active or invalidated. Based on that I need to decide whether to show Logout button on the page or not.

Although I have learnt that the session is maintained in the browser using session cookies, from this and this, I concluded that we have to manually set the variable values in the cookie object that we create in javascript.

Is there any way to get the session cookie that is used to municate with server? By getting that cookie, we can perhaps see whether the session is currently active or not.

Please let me know if my interpratation is wrong.

Share Improve this question edited May 23, 2017 at 12:00 CommunityBot 11 silver badge asked Aug 25, 2015 at 7:31 vishalakshvishalaksh 2,1745 gold badges29 silver badges46 bronze badges 4
  • That's not the way to go. You won't have access to the server's records on session existence. Anyone might create a local cookie with a session ID in it. – Joost Commented Aug 25, 2015 at 7:34
  • Why don't you have your view page output conditionally on whether or not there's a session? The cookie is not reliable because the server may have its own session timeout. – raduation Commented Aug 25, 2015 at 7:34
  • @raduation you mean to say that I should first query from the server for the existence of the session from each page and then decide to display logout button based on it? – vishalaksh Commented Aug 25, 2015 at 7:38
  • Yes, you should check on the server side whether there is an active session and do an if statement to show or not show the logout button. – raduation Commented Aug 25, 2015 at 7:39
Add a ment  | 

4 Answers 4

Reset to default 1

Generally you don't have access to session cookies from JavaScript. The simplest way is just to make an ajax request to the server to check if you have an authenticated session. Alternatively you can set a custom cookie with session info.

You can't check the session expire or not in java script using Session because if you write code like this:

if (isset($_SESSION["PHPSESSID"])) {}

that write on document it working properly but can not change value call of the javascript. if you reload a page than it update session value.

use ajax code PHP (session.php)

<?php
session_start();
if (isset($_SESSION['username'])) {
    echo 'success';
}

jquery:

$.ajax({
    url: 'path-to/session.php',
    type: 'POST',
    success: function(result) {
        if (result == 'success'){
            //...
        }else{
            //...
        }
    }
});

Actually there is a way you can use to check session via javascript, here is the code

$.get('path/to/session_check.php', function(data) {
 if( data == "Expired" ) {
     alert("Session expired");
 } else if (data == "Active" ) {
     alert("Session active");
 }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745104940a4611500.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信