JAVA Applet más sessiont használ, mint a böngésző - JAVA applet uses a new session when call PHP

Hungarian:

Amikor egy JAVA applet van egy oldalon (Flash is lehet) az nem ugyanazt a session Id-t használja, mint amit maga az oldal, ezért a szerver oldali PHP kód másik böngészőnek ismeri fel. A megoldás:

  • A PHP kód a web oldal legenerálásakor beírja az applet definíciójába az aktuális session azonosítót a session_id() függvény segítségével.
  • Amikor a JAVA applet elindul a weboldalról ezt a paramétert beolvassa a getParameter() függvénnye és letárolja egy String típusú változóban.
  • Amikor a JAVA meghívja a szerver oldali scriptet, az url-hez hozzáfűzi az aktuálisan lementett session Id-t.
  • A szerver oldali script figyeli, hogy a $_GET paraméterek között ott van-e egy megadott nevű paraméter és ha igen, akkor azzal a paraméterrel nyitja meg a session-t. Alul van egy kód a megoldás bemutatására.

English:
If you use a java applet in a page, the the applet does not uses the same session. It is the right behaviour, because security reasons. What is the solution:

  • The PHP code general the web page including java applet code you write in the page the actualy session_id with session_id() function.
  • When the JAVA applet starts it reads that session id from web page with getParameter() .
  • When the JAVA applet call a server side php script, in the url sends the session id to server in the url with a special parametername.
  • The server side listens the $_GET parameters, and when the special parameter exists the client open the session with that parameter.

Here is some code:

Applet code in html page:

<applet code="DataGrid" archive="DataGrid.jar"mayscript>
  ...
  <param name="SID" value="abcdef11376671d3evrh5qnvnvun5"> //actual session ID
</applet>

JAVA code in the applet:

private String SID;
....
SID = getParameter("SID");

...
URL url=null;
URLConnection con=null;
String sURL = "server.php?cmd=FUNC:MuszerRow:"+id+"&sid="+SID;
try {
  url = new URL(getCodeBase(), sURL);
  con = url.openConnection();
  con.connect();
  BufferedReader input = new BufferedReader(new InputStreamReader(con.getInputStream()));
  String s = input.readLine();  
  ...
}catch(Exception e){
   System.out.print("Error: " + e.toString() );
}

The server side script in server.php

<?php
  session_start();	
  if(isset($_GET["sid"]) && (session_id() != $_GET["sid"])){
	session_write_close();
	session_id($_GET["sid"]);
	session_start();
  }
....
?>

Valid XHTML 1.0 Transitional Valid CSS file Link firefox.hu pagerank

Design: © 2007-2011 Fábián Zoltán