-1

I am wanting to write a simple and straightforward client side JavaScript which will check if the users session is still active and then enable some action to take place, and if not for some other action to be coded for.

I have searched for a simple piece of JavaScript to achieve this, but not been successful to date.

halfer
  • 19,824
  • 17
  • 99
  • 186
Alexei
  • 31
  • 2
  • 4
  • Define **users session** as used here: _which will will check if the users session is still active_ – Randy Casburn Oct 16 '18 at 18:11
  • do you currently have something saving a session? If so is it using a cookie or localStorage? Each has its own case. – Pari Baker Oct 16 '18 at 18:12
  • Possible duplicate of [How to access Session variables and set them in javascript?](https://stackoverflow.com/questions/15519454/how-to-access-session-variables-and-set-them-in-javascript) – iff_or Oct 16 '18 at 21:41

2 Answers2

1

Use this code below

PHP

var session = '<?= session["username"] != null ?>';

.Net

var session = '<%=Session["username"] != null%>';

Java

var session = '<%= session.getAttribute("username") != null %>';

then follow it with this code

if(session){
        alert("Your sesssion is active ");  
        }
ABODE
  • 958
  • 2
  • 15
  • 13
0

maybe i dont follow your question good enough. but what the issue with doing something like that (it can be much more mature ):

if (sessionStorage.user) {
    console.log(1)
} else {
    console.log('user not exist in the session storage')
}