title: How can my java applet use my PHP authenticated session?
body: <p>I have set up a login area on my PHP server. The members.php file requires login; after I login a session is created. The session lasts for a while. I want to make this work so that while the session is still valid the java applet should be able to access members.php page.</p>
  
  <p>I have embedded the Java applet into the members.php page. It makes a HttpURLConnection request, however when I get the response I find that it was redirected by the PHP server to the login page.</p>
  
  <p><strong>How do I set this up correctly?</strong></p>
  
  <p>Here is the Java Applet code:</p>
  
  <pre><code>import java.applet.Applet;
  import java.awt.Dimension;
  import java.io.BufferedReader;
  import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLConnection;
  import java.util.List;
  import java.util.Map;
  
  import javax.swing.JFrame;
  import javax.swing.JTextArea;
  
  public class phpConnectApplet extends Applet {
  
      private static final long serialVersionUID = 1L;
  
       public void init() {
           URL url = null;
           try {
               url = new URL("http://www.example.com/members.php");
               URLConnection urlConn = url.openConnection();
               HttpURLConnection httpConn = (HttpURLConnection) urlConn;
               httpConn.setDoOutput(true);
               httpConn.setDoInput(true);
               httpConn.setUseCaches(false);
               httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
               DataOutputStream output = new DataOutputStream(httpConn.getOutputStream());
               String content = "action=blah"; //just to test the PHP file
               output.writeBytes(content);
               output.flush();
               output.close();
  
               DataInputStream in = new DataInputStream(urlConn.getInputStream());
               BufferedReader input = new BufferedReader(new InputStreamReader(in));
               String str, result = "";
               while ((str = input.readLine()) != null) {
                   result = result + str + "\n";
               }
               input.close();
  
               Map&lt;String, List&lt;String&gt;&gt; headers = httpConn.getHeaderFields(); 
               List&lt;String&gt; values = headers.get("Set-Cookie");
               String cookieValue = null; 
               for (String v:values) {
                    if (cookieValue == null)
                         cookieValue = v;
                     else
                         cookieValue = cookieValue + ";" + v;
               }
               System.out.println(cookieValue);
  
               JFrame f = new JFrame("App Title");
               f.add(new JTextArea(result));
               f.setMaximumSize(new Dimension(400,300));
               f.pack();
               f.setLocationRelativeTo(null);
               f.setVisible(true);
           } catch (MalformedURLException me) {
               me.printStackTrace();
           } catch(IOException ie) {
               ie.printStackTrace();
           } catch(Exception e) {
               e.printStackTrace();
           }
       }
  
  }
  </code></pre>
  
  <p>The output for this is a JFrame with one JTextField which contains the output HTML of the login page.</p>