【问题标题】:Using a session with php and Java使用 php 和 Java 会话
【发布时间】:2010-06-28 23:38:26
【问题描述】:

我目前正在尝试使用来自小程序的 php 网页的当前会话。我认为这将是直截了当的,但它并没有像我强硬那样顺利。来自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 发出请求,它返回给我会话 ID。当我在第 2 页上发出新请求时,我将会话 ID 作为参数传递,似乎没有保留会话。我用

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 方法,但它似乎不起作用。

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

谢谢。

【问题讨论】:

    标签: java php session applet


    【解决方案1】:

    接受会话 ID 作为 GET 的一部分是不好的形式,并且在安全方面也是个坏主意。我建议您从 PHPSESSION cookie 中检索会话 ID,例如:

    跟随 java sn-p 是无耻的 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 "";
        }
    

    在您的代码中的其他地方使用以下方法获取会话 ID:

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

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

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

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

    【讨论】:

    • 这可能是一个愚蠢的问题,但是通过 GET/POST 和 cookie 传递会话 ID 的安全性有什么区别? (另外,我在使用 post 时在 php 和 java 之间加密我的数据)
    【解决方案2】:

    您的 PAGE2.php 实际上并未使用您通过 _GET 传递的 sid 参数来启动会话。

    在page2.php中,尝试:

    session_id($_GET['sid']);
    session_start(); 
    

    而不是普通的:

    session_start();
    

    【讨论】:

    • 我一直在尝试,但没有成功。再次阅读手册页后,似乎 session_start 是无效的。所以不工作是正常的。
    • 你实际上是在寻找session_id($_GET['sid']); session_start();
    • 谢谢弗兰克!我误读了有关 session_id 的文档。我很难在调用它时总是创建一个新会话。现在它就像一个魅力!
    猜你喜欢
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 2019-10-28
    • 2019-07-14
    相关资源
    最近更新 更多