Javascript 如何使用 Ajax 函数从 php 服务器获取会话变量?(PHP HTML JS Ajax)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2765175/
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-23 01:50:08  来源:igfitidea点击:

How to get session variables from php server with Ajax function? (PHP HTML JS Ajax)

phpjavascripthtmlajaxsession

提问by Rella

so in my php I have something like this

所以在我的 php 我有这样的东西

$_SESSION['opened'] = true;

But It will not be set to true until user will perform some actions with some other html\php pages

但它不会被设置为 true,直到用户对其他一些 html\php 页面执行一些操作

So I need on some Ajax function to be able get this session variable. And some PHP sample of function to get variable in form ready for Ajax to get it.

所以我需要一些 Ajax 函数才能获得这个会话变量。以及一些 PHP 函数示例,用于在表单中获取变量,以便 Ajax 获取它。

so I need something to AJAX requesting to an action (to some simple php code) which will return a value from $_SESSION.

所以我需要一些东西到 AJAX 请求一个动作(到一些简单的 php 代码),它将从$_SESSION.

How to do such thing?

这样的事情怎么办?

回答by Matt

Simple jQueryexample:

简单的jQuery示例:

var session;
$.ajaxSetup({cache: false})
$.get('getsession.php', function (data) {
    session = data;
});

And getsession.php:

并且getsession.php

<?php
session_start();
print json_encode($_SESSION);

You're not requiredto use jQuery for AJAX, but I highly recommend it.

不需要为 AJAX 使用 jQuery,但我强烈推荐它。

Edit:

编辑:

In response to:

回应:

I want to be able to tell my JS function what variable I want to get.

我希望能够告诉我的 JS 函数我想获得什么变量。

You can try this (untested):

你可以试试这个(未经测试):

var session;
$.ajaxSetup({cache: false})
$.get('getsession.php', {requested: 'foo'}, function (data) {
    session = data;
});

And the PHP:

和 PHP:

<?php
session_start();
if (isset($_GET['requested'])) {
    // return requested value
    print $_SESSION[$_GET['requested']];
} else {
    // nothing requested, so return all values
    print json_encode($_SESSION);
}

Have a look at the $.getdocumentation for a more detailed overview.

查看$.get文档以获得更详细的概述。

回答by Aiden Bell

PHP File at http://my.host/response.php

PHP 文件位于http://my.host/response.php

<?php
session_start();
if(isset($_SESSION['opened']))
    echo "true";
?>

Then in HTML, Add jQuerysuch as:

然后在 HTML 中,添加jQuery如:

<script type="text/javascript" src="/path/to/jQuery-x.y.z.js"></script>

Then:

然后:

<script type="text/javascript">
    $(document).ready(function(){
        $.ajax({
            url:'/response.php',
            cache:false,
            success:function(data){
                // Do something with the result
                if(data=="true"){
                    $('#mydiv').show();
                }else{
                    $('#mydiv').hide();
                }
            }
        );
     });
</script>

And add to myform.php:

并添加到myform.php

<h1>Some Random HTML</h1>
<div id='mydiv' class="<?php if(isset($_SESSION['opened']) && $_SESSION['opened']) echo "hidden_class";?>">
 ...</div>

As this will give a consistent experience to those without JavaScript. You don't have to be showing/hiding a div. You could do anything really.

因为这将为那些没有 JavaScript 的人提供一致的体验。您不必显示/隐藏 div。你真的可以做任何事情。