java - 使用 php 和 Java session

标签 java php session applet

我目前正在尝试从小程序使用 php 网页的当前 session 。我以为这会很简单,但它并没有我想象的那么顺利。来自php man :

session_start() creates a session or resumes the current one based on a session
identifier passed via a GET or POST request, or passed via a cookie.

从那里我做了一些 php(这里简化了):

// PAGE1.PHP
session_start();
$_SESSION['test'] = true;
echo "sid=" . session_id();

// PAGE2.PHP
session_start();
if ($_SESSION['test'])
    $echo "success";
else
    $echo "fail";

因此,我从我的小程序向 PAGE1.PHP 发出请求,它返回 session ID。当我在第 2 页上执行新请求时,我将 session ID 作为参数传递,但似乎没有保留 session 。我用

URL url = new URL("my/url/PAGE2.php?sid=" + session_id); 
URLConnection conn = url.openConnection();
conn.setDoOutput(true); 
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 

wr.write(data); // data is the post data created previously
wr.flush(); 

// Get the response 
BufferedReader rd = new BufferedReader(
    new InputStreamReader(conn.getInputStream())); 
String line;
while ((line = rd.readLine()) != null) { 
    System.out.println(line);
}

我已经尝试通过 POST 和 GET 方法,但似乎没有用。

所以我想知道这是否可能,如果可能,我错过了什么?

谢谢。

最佳答案

接受 session ID 作为 GET 的一部分是一种错误的形式,也是一种不安全的想法。我建议您使用类似以下内容的 PHPSESSION cookie 检索 session ID:

下面的 java 片段是无耻的 copied from here – 看看那个(尽管它是特定于 Java 1.4 的)。

public String getCookie() {
  /*
  ** get all cookies for a document
  */
  try {
    JSObject myBrowser = (JSObject) JSObject.getWindow(this);
    JSObject myDocument =  (JSObject) myBrowser.getMember("document");
    String myCookie = (String)myDocument.getMember("cookie");
    if (myCookie.length() > 0) 
       return myCookie;
    }
  catch (Exception e){
    e.printStackTrace();
    }
  return "?";
  }

 public String getCookie(String name) {
   /*
   ** get a specific cookie by its name, parse the cookie.
   **    not used in this Applet but can be useful
   */
   String myCookie = getCookie();
   String search = name + "=";
   if (myCookie.length() > 0) {
      int offset = myCookie.indexOf(search);
      if (offset != -1) {
         offset += search.length();
         int end = myCookie.indexOf(";", offset);
         if (end == -1) end = myCookie.length();
         return myCookie.substring(offset,end);
         }
      else 
        System.out.println("Did not find cookie: "+name);
      }
    return "";
    }

在代码的其他地方使用以下方法获取 session ID:

  getCookie("PHPSESSION"); // replace this with the cookie name in your /etc/php.ini

并将其设置在您的小程序中。

 conn.setRequestProperty("Cookie", "PHPSESSION=value"); 

有关更多最新信息,请访问 sun java cookie page

关于java - 使用 php 和 Java session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3136898/

相关文章:

java - 快速排序和优化快速排序有什么区别?

php - 警告 : session_start(): failed: Permission denied (13)

php - codeigniter count_all_results 与连接表

php - 使用 __Secure-/__Host- 前缀重命名 PHP session cookie

json - res.json 发送后无法设置 header

ruby - 在 Sinatra 中间件中访问 session

java - Android 8.0 Oreo 输入类型 numberPassword 不起作用 - 可见

java - AWS : There is insufficient memory for the Java Runtime Environment to continue

java - 了解 Java 中的对象反射优势

javascript - 将变量保留在 while 循环的最后一次迭代中