开发者

PHP Session variables in java

In my server-clien开发者_StackOverflow中文版t model I'm using java for client side and for server side scripting using php5.

For communication I'm using simple http protocol.

In server I have some $SESSION variables(in php). I want access these variables in my java client.

How can it be posssible?

Thanks in advance.


AFAIK session variables is a server-side variables, that can be accessed by session id. You must refactor you application (to make java-client free of server side information) or send them (variables) from server to client manually.


You should use cookies instead of session variables if you're planning on sending it to the client side.


Your Java-client will make requests to the server, and the server will act like a web service, right?

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 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.
Thus, you have to request it from PHP as well.
Of course, supplying a request with session identifier got from the previous calls. That's all, not a rocket science.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜