如何从PHP服务器使用Ajax功能获取会话变量? (PHP HTML JS阿贾克斯) [英] How to get session variables from php server with Ajax function? (PHP HTML JS Ajax)
问题描述
所以在我的PHP我有这样的事情
so in my php I have something like this
$_SESSION['opened'] = true;
但它不会被设置为true,直到用户执行某些动作与其他一些HTML \ PHP页面
But It will not be set to true until user will perform some actions with some other html\php pages
所以,我需要一些Ajax功能,能够得到这个会话变量。与函数一些PHP样本来获得变量的形式准备Ajax来得到它。
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 code),这将从 $返回值_ SESSION
。
so I need something to AJAX requesting to an action (to some simple php code) which will return a value from $_SESSION
.
如何做这样的事?
推荐答案
简单 jQuery的例如:
var session;
$.ajaxSetup({cache: false})
$.get('getsession.php', function (data) {
session = data;
});
和 getsession.php
:
<?php
session_start();
print json_encode($_SESSION);
您是不是的需要的使用jQuery的AJAX,但我强烈推荐它。
You're not required to use jQuery for AJAX, but I highly recommend it.
编辑:
在响应:
我希望能够告诉我的JS功能,我希望得到什么变化。
I want to be able to tell my JS function what variable I want to get.
您可以试试这个(未经测试):
You can try this (untested):
var session;
$.ajaxSetup({cache: false})
$.get('getsession.php', {requested: 'foo'}, function (data) {
session = data;
});
和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);
}
看一看更详细的概述 $不用彷徨的文档。
这篇关于如何从PHP服务器使用Ajax功能获取会话变量? (PHP HTML JS阿贾克斯)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!