douying1119 2016-12-30 06:44
浏览 29
已采纳

如何在javascript中访问php SESSION变量[重复]

This question already has an answer here:

I'm trying to create an image uploading website. In this, when a user logs in, I set a $_SESSION['id'] variable in php. Now, I wish to check if $_SESSION['id'] is set in my javascript file (general.js) in order to carry out certain functions. How should I go about doing it?

</div>
  • 写回答

4条回答 默认 最新

  • dth2331 2016-12-30 06:58
    关注
    <?php $session_value=(isset($_SESSION['id']))?$_SESSION['id']:''; ?>
        <html>
        <head>
        <script type="text/javascript">
        var myvar='<?php echo $session_value;?>';
        </script>
        <script type="text/javascript" src="general.js"></script>
        </head>
        <body>
    
        </body>
        </html>
    

    In above code snippet I have initialized global variable myvar with value stored into php session variable into script file before general.js file is referenced in script.It will be accessible in general.js file

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?