Get Current Session Value in JavaScript?

后端 未结 11 1562
深忆病人
深忆病人 2021-02-05 01:10

I have a scenario where I open my web application in a browser but in two separate tabs.

In one tab I signed out from the application and as a result the all session val

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-05 01:46

    i tested this method and it worked for me. hope its useful.

    assuming that you have a file named index.php
    and when the user logs in, you store the username in a php session; ex. $_SESSION['username'];

    you can do something like this in you index.php file

    
    

    now you can access the variable userId in another script block placed in the index.php file, or even in a .js file linked to the index.php

    ex. in the index.js file

    $(document).ready(function(){
        alert(userId);   
    })
    

    it could be very useful, since it enables us to use the username and other user data stoerd as cookies in ajax queries, for updating database tables and such.

    if you dont want to use a javascript variable to contain the info, you can use an input with "type='hidden';

    ex. in your index.php file, write:

    ";
    ?>
    

    however, this way the user can see the hidden input if they ask the browser to show the source code of the page. in the source view, the hidden input is visible with its content. you may find that undesireble.

提交回复
热议问题