从 JavaScript 访问会话变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8142502/
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
Access session variables from JavaScript
提问by H Bellamy
Is it possible to access session variables made in from JavaScript, and how can I replicate this PHP code in JavaScript?
是否可以访问由 JavaScript 生成的会话变量,以及如何在 JavaScript 中复制此 PHP 代码?
if(empty($_SESSION['name'] {
echo 'foo';
}
采纳答案by Eliseu Monar dos Santos
You could access a page via Ajax which would return the variable value:
您可以通过 Ajax 访问页面,该页面将返回变量值:
Using jQuery:
使用jQuery:
$.get('myVariable.php', function ( data ) {
alert(data)
});
And the PHP page (myVariable.php):
还有 PHP 页面 (myVariable.php):
<?php echo $_SESSION['user_id']; ?>
回答by Mike Purcell
If you setup your javascript to be parsed by php, you can access session vars just like you can in php:
如果您将 javascript 设置为由 php 解析,则可以像在 php 中一样访问会话变量:
// Test.php
<?php session_start(); ?>
<?php $_SESSION['foo'] = 'bar' ?>
<script type="text/javascript">
alert('<?php echo $_SESSION['foo'] ?>');
</scrip>
<?php var_dump($_SESSION['foo']) ?>
回答by Tomas
Sorry but you can't :(
对不起,你不能:(
First a little Background:
首先介绍一下背景:
Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site.
A visitor accessing your web site is assigned a unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.
The session support allows you to register arbitrary numbers of variables to be preserved across requests. When a visitor accesses your site, PHP will check automatically (if session.auto_start is set to 1) or on your request (explicitly through session_start() or implicitly through session_register()) whether a specific session id has been sent with the request. If this is the case, the prior saved environment is recreated. From http://www.php.net/manual/en/intro.session.php
PHP 中的会话支持包括一种在后续访问中保留某些数据的方法。这使您能够构建更多定制的应用程序并增加您网站的吸引力。
访问您网站的访问者会被分配一个唯一的 ID,即所谓的会话 ID。这要么存储在用户端的 cookie 中,要么在 URL 中传播。
会话支持允许您注册任意数量的变量以跨请求保留。当访问者访问您的站点时,PHP 将自动检查(如果 session.auto_start 设置为 1)或根据您的请求(通过 session_start() 显式或通过 session_register() 隐式)检查是否已随请求发送了特定的会话 ID。如果是这种情况,则会重新创建先前保存的环境。 来自http://www.php.net/manual/en/intro.session.php
Basically Sessions involve storing data server side and storing an ID in a cookie client side. Meaning that JS could in theory access the cookie but then all you would have would be access to the PHPSESSID variable (the session id).
基本上会话涉及存储数据服务器端和在 cookie 客户端存储一个 ID。这意味着 JS 理论上可以访问 cookie,但随后您将只能访问 PHPSESSID 变量(会话 ID)。
To achieve what you are trying to do, you can use ajax. (Contrary to the popular misconception) ajax requests still provide the server with all the information it needs to access cookies and sessions.
为了实现您想要做的事情,您可以使用 ajax。(与流行的误解相反)ajax 请求仍然为服务器提供访问 cookie 和会话所需的所有信息。
So simply make a server side script like getName.php
which contains:
因此,只需制作一个服务器端脚本getName.php
,其中包含:
if(!empty($_SESSION['name'])) {
echo $_SESSION['name'];
}
And use ajax to get the response :)
并使用 ajax 获取响应:)
回答by Landon
well ... ajax is nice and everything, but if you really want, you can just print them as variables in your tag in the head and access them via js that way. I've never done that with session variables, but I print out values from php (mysql) in that manner all the time
嗯... ajax 很好,一切都很好,但是如果你真的想要,你可以将它们作为变量打印在你的头部标签中,然后通过 js 访问它们。我从来没有用会话变量这样做过,但我一直以这种方式从 php (mysql) 中打印出值
回答by Darin Dimitrov
is it possible to access session variables made in php from javascript
是否可以从 javascript 访问在 php 中创建的会话变量
No. They reside in the memory of your web server to which, fortunately, javascript has no access.
不。它们驻留在您的网络服务器的内存中,幸运的是,javascript 无法访问。
回答by James Montagne
Session variables are stored on the server. The only way to access them with javascript (which is client side) would be to make a request to the backend with AJAX to a php page which would then return the value to you.
会话变量存储在服务器上。使用 javascript(客户端)访问它们的唯一方法是使用 AJAX 向后端发出请求到一个 php 页面,然后将值返回给您。
回答by Galled
No you cant. Session variables are stored in the server, not in the client. The closest thing you can do is verify the session variable in a php script and call it with ajax.
不,你不能。会话变量存储在服务器中,而不是客户端中。您可以做的最接近的事情是验证 php 脚本中的会话变量并使用 ajax 调用它。