'PHP Session variables in java
In my server-client 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.
Solution 1:[1]
You should use cookies instead of session variables if you're planning on sending it to the client side.
Solution 2:[2]
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?
Solution 3:[3]
Simple.
<script type="text/javascript">
var variable = '<?php echo $SESSION["variable_name"];?>';
</script>
Solution 4:[4]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | PatrikAkerstrand |
| Solution 3 | adarshr |
| Solution 4 | Your Common Sense |
