简体   繁体   English

java中的PHP会话变量

[英]PHP Session variables in java

In my server-client model I'm using java for client side and for server side scripting using php5.在我的服务器-客户端模型中,我使用 java 作为客户端,使用 php5 进行服务器端脚本。

For communication I'm using simple http protocol.对于通信,我使用简单的 http 协议。

In server I have some $SESSION variables(in php).在服务器中,我有一些 $SESSION 变量(在 php 中)。 I want access these variables in my java client.我想在我的 java 客户端中访问这些变量。

How can it be posssible?怎么可能?

Thanks in advance.提前致谢。

AFAIK session variables is a server-side variables, that can be accessed by session id. AFAIK 会话变量是服务器端变量,可以通过会话 ID 访问。 You must refactor you application (to make java-client free of server side information) or send them (variables) from server to client manually.您必须重构您的应用程序(使 java-client 没有服务器端信息)或手动将它们(变量)从服务器发送到客户端。

如果您打算将其发送到客户端,则应使用cookie而不是会话变量。

Your Java-client will make requests to the server, and the server will act like a web service, right?您的 Java 客户端将向服务器发出请求,而服务器将像 Web 服务一样运行,对吗?

If so, include all/any $_SESSION variables you might need with the XML/json/whatever response that the server sends back to the clients, something along the lines of:如果是这样,请在服务器发送回客户端的 XML/json/whatever 响应中包含您可能需要的所有/任何 $_SESSION 变量,类似于:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<reply>
  <request>
    <method>getUsers</method>
    <param name="page">1</param>
    <param name="apiKey">API_KEY</param>
    <param name="sessId">SESSION_ID</param>
  </request>
  <response>
    <status>
      <code>0</code>
      <message>OK</message>
    </status>
    <users>
      <!-- Request reply here -->
    </users>
    <session>
      <variable1>$_SESSION['var1']</variable1>
      <!-- Additional session variables you might need on the client here[...] -->
    </session>
  </response>
</reply>

The thing is, session variables are something the server uses to keep track of which client is accessing it now, and relate data to that particular client.问题是,会话变量是服务器用来跟踪哪个客户端现在正在访问它,并将数据与该特定客户端相关联的东西。 If you need this information on the client, perhaps you could refactor the application to create this data on the client and pass it to the server in case the server needs that data?如果您需要客户端上的此信息,也许您可​​以重构应用程序以在客户端上创建此数据并将其传递给服务器以防服务器需要该数据?

Simple.简单的。

<script type="text/javascript">
    var variable = '<?php echo $SESSION["variable_name"];?>';
</script>

The only thing you have to understand is that session variables has no difference from any other PHP variable.您唯一需要了解的是会话变量与任何其他 PHP 变量没有区别。
Thus, you have to request it from PHP as well.因此,您也必须从 PHP 请求它。
Of course, supplying a request with session identifier got from the previous calls.当然,提供带有会话标识符的请求是从先前的调用中获得的。 That's all, not a rocket science.仅此而已,不是火箭科学。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM