如何在php app和Java EE app之间共享会话? [英] How to share session between php app and Java EE app?

查看:126
本文介绍了如何在php app和Java EE app之间共享会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可能会用Java EE应用程序替换PHP应用程序,但问题是我们要逐个替换模块,这意味着两个应用程序将共存并相互通信。

We may replace a PHP app with a Java EE app, but the problem is we wanna replace the modules one by one, which means two apps would co-exist and communicate with each other.

那么可以在两个应用之间共享用户会话吗?或使用cookie来解决问题?

So is it possible to share the user session between the 2 apps? Or use a cookie to solve the problem?

推荐答案

使用 session_set_save_handler()

更新
效率明智的,它将是非常小的,与读取文本文件到查询数据库(可能使用现有连接)的区别。

UPDATE Efficiency wise it would be very mininal, the difference from reading a text file to querying a database (presumably using an existing connection).

一些示例代码(简化了我使用的内容) )

Some example code (simplified from what I use)

class Session {
    public function __construct() {
        session_start();       
        $this->clean();        
    }
    public function __destruct() {
        session_write_close();
    }
    public function open() {
        return TRUE;
    }    
    public function close() {
        return TRUE;
    }    
    public function read($id) {
        global $Database;
        if ($Database->select('session_data FROM sessions WHERE session_id="%s"', $id)) {
            list($data) = $Database->fetch(MYSQL_NUM);
            return $data;
        } else {
            return '';
        }
    }    
    public function write($id, $data) {
        global $Database;
        return $Database->replace('sessions SET session_id="%s", session_data="%s", session_updated=%d', array($id, $data, time()));
    }    
    public function destroy($id) {
        global $Database;
        $_SESSION = array();
        return $db->delete('sessions WHERE session_id="%s"', $id);
    }    
    public function clean($expire = 600) {
        global $Database;
        $Database->delete('FROM sessions WHERE session_updated<%d', $time - $expire);
        return TRUE;
    }
}
// Declare the functions to use:
session_set_save_handler(array('Session', 'open'), array('Session', 'close'), array('Session', 'read'), array('Session', 'write'), array('Session', 'destroy'), array('Session', 'clean'));
$Session = new Session;

您可以使用 $ _ SESSION ['读取/写入会话数据名称'] =数据; 采用通常的技术。

The you can read/write to the session data using $_SESSION['name'] = data; in the usual technique.

这篇关于如何在php app和Java EE app之间共享会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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