Javascript 从 Jquery 获取 PHP $_SESSION 变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11911779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:38:17  来源:igfitidea点击:

Get a PHP $_SESSION variable from Jquery?

phpjavascriptjquerysession

提问by khaverim

I'm not yet a JSON/AJAX master so I don't know how to do this.

我还不是 JSON/AJAX 大师,所以我不知道如何做到这一点。

I need a $_SESSION['name'] PHP variable to work with in my jQuery stuff and I don't know how to access it... consider:

我需要一个 $_SESSION['name'] PHP 变量来处理我的 jQuery 内容,但我不知道如何访问它......考虑:

// the 'who is typing' shindig
    $.ajax(
    {
        url: "whos_typing.html",
        cache: false,
        success: function(whos)
        {   
                // here I need to access $_SESSION['name'] and do stuff with it

            $("#soandso").html(whos); //Insert who's typing into the #soandso       
        }
        });

回答by Makita

You'll need to inject it, something like this:

你需要注入它,像这样:

var sessName = '<?php echo $_SESSION['name']?>';

The file containing this script must be executed by the php interpreter (i.e. a .php file)

包含此脚本的文件必须由 php 解释器执行(即 .php 文件)

EDIT: Conceding to Radu's point, it would be safer execute for unsanitized data:

编辑:承认 Radu 的观点,对未经消毒的数据执行会更安全:

var sessName = <?php echo json_encode($_SESSION['name']) ?>;

回答by Greg Rozmarynowycz

You need to use $.postto retrieve the variable from the server. You would have something like this:

您需要使用$.post从服务器检索变量。你会有这样的事情:

 $.post('echoMyVar.php', {post: 1}, function(data){
      myVar = data['myVar'];
      });

This is very basic, you first need to check if datais not null. In echoMyVar.php, you need just need basically the following:

这是非常基本的,您首先需要检查是否data不为空。在 echoMyVar.php 中,您基本上只需要以下内容:

 header('Content: application/json', 1);

 $returnVal = array('myVar', $_SESSION['myVar']);

 echo json_encode($returnVal);

Again this is a shell, not secure, and would not handle any errors.

同样,这是一个外壳,不安全,不会处理任何错误。

回答by palerdot

var name= "<?php echo $_SESSION['user_name'];?>"

will do it . . .

会做的 。. .

Rememberphp is a server side script, . . .so it takes precedence and get executed first and spits html to the client (Jquery , javacript) which will be executed in your browser . . . .

记住php 是一个服务器端脚本,. . .so 它优先并首先执行并将 html 吐出到将在您的浏览器中执行的客户端(Jquery,javacript)。. . .

So, you can use server side variables to share with client . . . but not the other way around . . .

因此,您可以使用服务器端变量与客户端共享。. . 但反过来不行。. .

回答by Alex Kalicki

The easiest way is probably to include your javascript code in a .php file. Then you can simply do:

最简单的方法可能是将您的 javascript 代码包含在 .php 文件中。然后你可以简单地做:

var phpVar = <?php echo $_SESSION['name']; ?>

SIMILAR POST

类似的帖子

回答by Eugene Naydenov

Server side in whos_typing.php:

whos_typing.php 中的服务器端:

<?php
//...
header('Content-Type: application/json');
echo json_encode(array(
    'who'=>'Bob',
    'session'=>$_SESSION,
    // Be sure that you're not storing any sensitive data in $_SESSION.
    // Better is to create an array with the data you need on client side:
    // 'session'=>array('user_id'=>$_SESSION['user_id'], /*etc.*/),
));
exit(0);

Client side:

客户端:

// the 'who is typing' shindig
$.ajax({
    url: "whos_typing.php",
    dataType: 'json',
    cache: false,
    success: function(data) {
        var session = data.session,
            who = data.who;
            console.log(session.user_id); // deal with session
        $("#soandso").html(who); //Insert who's typing into the #soandso
    }
});

回答by Jordan

You need to echothe session variable from PHP when you send it to the browser. I'm assuming whos_typing.html is just the URL to a PHP script.

echo当您将会话变量发送到浏览器时,您需要从 PHP 获取会话变量。我假设 whos_typing.html 只是 PHP 脚本的 URL。