使用PHP和Java的会话 [英] Using a session with php and Java

查看:180
本文介绍了使用PHP和Java的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用PHP网页本届会议从applet。我因子评分这将是直接的,但它并没有去顺利,因为我艰难。从 PHP的人

I'm currently trying to use the current session of a php web page from an applet. I tought it would be straightforward, but it didn't go as smooth as I tough. From the 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(这里简单):

From there I did some php (simplified here):

// 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作为一个参数,它似乎表明该会话没有跟上。我用

So, from my applet, I do a request to PAGE1.PHP and it returns me the session id. When I do a new request on the page 2, I pass the session id as a parameter and it seems that the session wasn't kept. I use

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方法,它似乎并没有工作。

I have tried via POST and GET method and it doesn't seem to work.

所以我想知道如果这是可能的,如果是,我该怎么错过?

So I'm wondering if it's possible, and if yes, what do I miss?

感谢。

推荐答案

您page2.php中将不实际使用您通过_GET传递SID参数来启动会话。

Your PAGE2.php is not actually using the sid param you're passing via _GET to initiate the session.

在page2.php中将,请尝试:

In page2.php, try:

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

而不是简单的历史:

instead of plain-old:

session_start();

这篇关于使用PHP和Java的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆