简体   繁体   English

如何在php应用程序与Java EE应用程序之间共享会话?

[英]How to share session between php app and Java EE app?

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. 我们可能用Java EE应用程序替换了一个PHP应用程序,但是问题是我们要一个接一个地替换模块,这意味着两个应用程序将共存并相互通信。

So is it possible to share the user session between the 2 apps? 那么可以在两个应用程序之间共享用户会话吗? Or use a cookie to solve the problem? 还是使用cookie来解决问题?

Sharing a regular Java EE session with PHP can be done very efficient and fast with PHP-java-bridge . 与PHP共享常规Java EE会话可以通过PHP-java-bridge高效而快速地完成。

This solution offer superior performance over using a database as it does not generate any disk IO. 与使用数据库相比,此解决方案提供了卓越的性能,因为它不会生成任何磁盘IO。 It also does not need any changes on the PHP webserver or Java EE server. 它还不需要在PHP Web服务器或Java EE服务器上进行任何更改。 Just add some code, configure and you're done. 只需添加一些代码,配置即可完成。

Setting up the php-java-bridge can be confusing, but if you know how to do it, it's only a 10-minute job. 设置php-java-bridge可能会令人困惑,但是如果您知道该怎么做,那只是10分钟的工作。 As I just did a proof of concept my self I can hand you the cookbook solutions: 正如我刚刚做的概念验证一样,我可以为您提供食谱解决方案:

  1. Download PHP-java-bridge files. 下载PHP-java-bridge文件。 I downloaded JavaBridgeTemplate610.zip (for the needed jar files) and php-java-bridge_6.1.0_documentation.zip for the needed examples, php include file and sample code (session sharing!). 我下载了JavaBridgeTemplate610.zip(用于所需的jar文件)和php-java-bridge_6.1.0_documentation.zip以获取所需的示例,php包括文件和示例代码(会话共享!)。

  2. Add the "JavaBridge.jar", "php-script.jar" and "php-servlet.jar" to you're webapp by putting it in the "/WEB-INF/lib/" of you're Java EE server. 将“ JavaBridge.jar”,“ php-script.jar”和“ php-servlet.jar”添加到您的webapp中,方法是将其放入Java EE服务器的“ / WEB-INF / lib /”中。

  3. Add a "test.jsp" to you're Java EE servers "/web" directory: 将“ test.jsp”添加到您的Java EE服务器的“ / web”目录中:

     <HTML> <TITLE>PHP and JSP session sharing</title> <BODY> <% javax.servlet.http.HttpSession $session = request.getSession(); if($session.getAttribute("counter")==null) { $session.setAttribute("counter", new java.lang.Integer(1)); } int $counter = ((java.lang.Integer)$session.getAttribute("counter")).intValue(); out.println ("HttpSession variable \\"counter\\": " + $counter + "<br>"); java.lang.Integer $next = new java.lang.Integer($counter+1); session.setAttribute("counter", $next); %> <a href="http://127.0.0.1/test.php">PHP page</a> </BODY> </HTML> 
  4. Configure the JavaBridge servlet so it can be used by PHP to communicate to the Java EE server. 配置JavaBridge Servlet,以便PHP可以使用它与Java EE服务器进行通信。 Just add the following lines to you're web.xml: 只需将以下几行添加到您的web.xml中:

     <servlet> <servlet-name>PhpJavaServlet</servlet-name> <servlet-class>php.java.servlet.PhpJavaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>PhpJavaServlet</servlet-name> <url-pattern>*.phpjavabridge</url-pattern> </servlet-mapping> 
  5. Add a file named "test.php" to you're webserver root and make sure you edit the JAVA_HOSTS and JAVA_SERVLET to correctly point to the Javabridgeservlet as configured in the web.xml. 将一个名为“ test.php”的文件添加到您的Web服务器根目录,并确保您编辑JAVA_HOSTS和JAVA_SERVLET以正确指向web.xml中配置的Javabridgeservlet。

     <?php define ("JAVA_HOSTS", "127.0.0.1:8084"); define ("JAVA_SERVLET", "/JavaBridge.phpjavabridge"); require_once("java/Java.inc"); $session = java_session(); ?> <HTML> <TITLE>PHP and JSP session sharing</title> <BODY> <?php if(is_null(java_values($session->get("counter")))) { $session->put("counter", 1); } $counter = java_values($session->get("counter")); print "HttpSession variable \\"counter\\": $counter<br>\\n"; $session->put("counter", $counter+1); ?> <a href="http://127.0.0.1:8084/test.jsp">JSP page</a> </BODY> </HTML> 
  6. Install the needed "java.inc" php include file. 安装所需的“ java.inc” php包含文件。 You will find the file in the downloaded "php-java-bridge_6.1.0_documentation.zip" in the src.zip. 您可以在src.zip的下载的“ php-java-bridge_6.1.0_documentation.zip”中找到该文件。 Copy the "java.inc" file in the "/java" directory (just this one php file!). 复制“ / java”目录中的“ java.inc”文件(仅此一个php文件!)。

  7. Restart Application server 重新启动应用程序服务器

  8. Start the test.php script (for example goto: http://127.0.0.1/test.php ) 启动test.php脚本(例如goto: http : //127.0.0.1/test.php

If you click on the links to the jsp and php file back, you will notice that the counter shares the Java session between both JSP and PHP scripts! 如果单击指向jsp和php文件的链接,您会发现计数器在JSP和PHP脚本之间共享Java会话!

In order to share the same JSession cookie in a JSP/Servlet and PHP they both need to run on the same domain name (also make sure that JAVA_HOSTS is the PHP file uses the same domain name!). 为了在JSP / Servlet和PHP中共享相同的JSession cookie,它们都需要在相同的域名上运行(还要确保JAVA_HOSTS是PHP文件使用相同的域名!)。

您可能要看的一个选项是Quercus ,它与Resin绑定在一起,可以让您在Java EE应用服务器上运行PHP代码,并可以在两个平台之间进行一些通信。

Save your session data to a database with session_set_save_handler() . 使用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). UPDATE效率方面,它是非常微小的,从读取文本文件到查询数据库(大概使用现有连接)是不同的。

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;

The you can read/write to the session data using $_SESSION['name'] = data; 您可以使用$_SESSION['name'] = data;读取/写入会话$_SESSION['name'] = data; in the usual technique. 在通常的技术中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM