简体   繁体   English

如何在PHP和ASP.net应用程序之间共享会话?

[英]How to share sessions between PHP and ASP.net application?

My company took some old php application over. 我的公司接管了一些旧的php应用程序。 Due to our preference to ASP.net and to the lack of any documentation from the previous developer, we do not want to spend much resources on developing in PHP. 由于我们偏爱ASP.net且由于以前的开发人员缺少任何文档,因此我们不想在PHP开发上花费大量资源。 For implementing new features, we will create an Asp.net application that has the same look to the user. 为了实现新功能,我们将创建一个Asp.net应用程序,其外观与用户相同。 We want to develop a kind of 'coexisting' web application. 我们要开发一种“共存”的Web应用程序。 Therefore we must share sessions between an PHP and an Asp.net webapplication project, because there is a usermanagement involved with an existing MySQL database. 因此,我们必须在PHP和Asp.net Web应用程序项目之间共享会话,因为现有的MySQL数据库涉及用户管理。

(eg link 'A' directs to the PHP website, and link 'B' directs to the asp.net application) (例如,链接“ A”指向PHP网站,链接“ B”指向asp.net应用程序)

How can we share the session between and PHP and an asp.net application? 我们如何在PHP和asp.net应用程序之间共享会话?

And does anyone have a hint for this 'coexisting' thing, that might be useful in development? 有没有人暗示这种“共存”的东西,这可能对开发有用吗?

Edit : IIS 6 would be our targeted server, altough IIS 7.5 would also be an option 编辑 :IIS 6将是我们的目标服务器,虽然IIS 7.5也是一个选择

I want to tell you, how I ended up doing it. 我想告诉你,我最终是怎么做到的。

Both applications access a MySQL database and access a "session" table, which consists of a Guid, the ID of the user, and a confirmationString (I guess I encoded the IDUser in it, somehow) and a date. 这两个应用程序都访问一个MySQL数据库并访问一个“会话”表,该表由一个Guid,该用户的ID和一个ConfirmationString(我想是在其中以某种方式对IDUser进行了编码)和一个日期组成。

Sessions are only started by the PHP application (due to the fact, that the PHP application is still the main application). 会话仅由PHP应用程序启动(事实上,PHP应用程序仍是主要应用程序)。 A new session will result in a new entry in the log table. 新的会话将在日志表中产生一个新条目。 Every link in the PHP application, that links to the ASP.Net application contains GET-Parameters, containing the Guid etc. PHP应用程序中的每个链接到ASP.Net应用程序的链接都包含GET-Parameters,包含Guid等。

The ASP.net application checks for the GET-Parameters and sets the IDUser in the ASP.Net Session, if the GET-Parameters point to an existing session. 如果GET-Parameters指向现有会话,则ASP.net应用程序将检查GET-Parameters并在ASP.Net会话中设置IDUser。

The links pointing back to the PHP application use the same technique. 指向PHP应用程序的链接使用相同的技术。

(There are also other things to consider, like timeouts or logouts, but that can be handled as well) (还有其他事情要考虑,例如超时或注销,但也可以处理)

All in all, I'd say that my approach is useful and none of the customers complained since the deployment (over 1 year ago) 总而言之,我想说我的方法很有用,自部署以来(一年多以前),没有客户抱怨

This is an old question, but I didn't think any of the current answers were complete. 这是一个古老的问题,但我认为当前的答案都不完整。

First, I think it is a bad idea to store session data in a database server like mysql or SQL Server. 首先,我认为将会话数据存储在mysql或SQL Server等数据库服务器中是个坏主意。 The DB is a precious resource, and there's really no reason to thrash it just for session data. 数据库是一种宝贵的资源,实际上没有理由仅将其用于会话数据。

If you are going to store session in a database like that, there are "better" ways of doing it, like making sure that the session data is on it's own independent disk, etc... but honestly, I still feel like it's a mistake and will limit your scalability. 如果您要将会话存储在这样的数据库中,则可以采用“更好”的方式,例如确保会话数据位于其自己的独立磁盘上,等等。但是说实话,我仍然觉得这是一个错误,并会限制您的可扩展性。

For storing session, you want to go with a simple key/value store, and in my opinion you can't beat memcached (though I've also had good luck with redis + nodejs). 对于存储会话,您想使用一个简单的键/值存储,并且我认为您无法击败memcached(尽管我也对Redis + nodejs感到很幸运)。

memcached has clients available for pretty much every language on earth: http://code.google.com/p/memcached/wiki/Clients memcached的客户几乎可以使用地球上的每种语言: http : //code.google.com/p/memcached/wiki/Clients

So, basically all you need to do when using memcached is generate a pseudo-random token for the key, and do a memcached.set. 因此,使用memcached时,基本上您需要做的就是为密钥生成一个伪随机令牌,然后执行memcached.set。 Then store that key in a cookie called session-id or something. 然后将该密钥存储在一个名为session-id的cookie中。

The session-id cookie can be read from any server language, .net, php, python, whatever - and the session value retrieved with a simple memcached.get. 可以从任何服务器语言,.net,php,python等中读取会话ID cookie,并且可以使用简单的memcached.get检索会话值。

Check out the memcached docs: http://code.google.com/p/memcached/wiki/NewStart 查看memcached文档: http : //code.google.com/p/memcached/wiki/NewStart

This is a very simple and scalable way to do sessions, and will work with almost any language/server. 这是一种非常简单且可扩展的会话方式,几乎可以与任何语言/服务器一起使用。

I don't think it's natively possible to share sessions between PHP and ASP.NET. 我不认为在PHP和ASP.NET之间共享会话本身是不可能的。

However, it might be possible by using a PHP page that reads the contents of the session, stores them in hidden fields and then call an ASP.NET page that would read these fields and load them into ASP.NET session. 但是,通过使用PHP页面读取会话的内容,将它们存储在隐藏字段中,然后调用ASP.NET页面,这将读取这些字段并将其加载到ASP.NET会话中,这是可能的。

Theoretically it's possible. 从理论上讲这是可能的。

http://cz.php.net/manual/en/function.session-set-save-handler.php http://cz.php.net/manual/en/function.session-set-save-handler.php
http://support.microsoft.com/kb/317604 http://support.microsoft.com/kb/317604

You can write PHP's sessions into MsSQL, and configure .NET to use MsSQL as backend for sessions. 您可以将PHP的会话写入MsSQL,并配置.NET以将MsSQL用作会话的后端。

Not a big deal. 没什么大不了的。
Your asp app should do a three simple things: 您的asp应用程序应执行以下三个简单操作:

  • Recieve a sessionid cookie from the client 从客户端接收会话ID cookie
  • look for the sess_<id> file in the PHP session save path 在PHP会话保存路径中查找sess_<id>文件
  • implement a PHP serialize/unserialize functions to read/write session data. 实现PHP序列化/反序列化功能以读取/写入会话数据。

A nicer way than just hacking into session storage mechanisms on both sides would be setting up OpenId provider and plugging OpenId consumers to both asp.net and php applications. 除了仅侵入双方的会话存储机制之外,更好的方法是设置OpenId提供程序并将OpenId使用者插入到asp.net和php应用程序中。

There's lot of existing code to do it. 有很多现有的代码可以做到这一点。 It would be both more elegant and error prone than the low level solutions. 与低级解决方案相比,它既优雅又容易出错。 As a bonus you could use integrated OpenId login in the rest of your company applications and become a company hero. 另外,您可以在公司的其他应用程序中使用集成的OpenId登录并成为公司的英雄。

See: Using OpenID for both .NET/Windows and PHP/Linux/Apache web sites 请参阅: 将OpenID用于.NET / Windows和PHP / Linux / Apache网站

Oh dear, maybe one day you'll see the error of your ways, in the meantime..... 哦,亲爱的,也许有一天您会发现自己的方式有误,.....

By default, PHP writes its session data as a serialized array into a file named according to the session. 默认情况下,PHP将其会话数据作为序列化数组写入根据会话命名的文件中。 The session is identified usually by a cookie with name PHPSESSID. 会话通常由名称为PHPSESSID的cookie标识。

So in PHP to manually read the session: 因此在PHP中手动读取会话:

$imported_session=unserialize(file_get_contents(session_save_path() . '/' . $_COOKIE[session_name()]));

The format of the file is very straightforward and simple to parse. 该文件的格式非常简单易懂。

However its quite easy to implement your own PHP session handler to write the files in any format/to any storage you like (have a look at auto-prepend for how to associate the revosed code with every page without having to rewrite each one). 但是,实现自己的PHP会话处理程序以任何格式/将文件写入所需的任何存储非常容易(请查看自动前缀如何将已删除的代码与每个页面相关联而不必重写每个页面)。 Or change the name the cookie used to store the session. 或更改用于存储会话的cookie的名称。

C. C。

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

相关问题 如何在同一台机器和同一应用程序池中的 ASP.NET Web 应用程序之间共享会话? - How to share sessions between ASP.NET Web Apps in same machine and same application pool? 如何在ASP.NET MVC 5应用程序和.NET控制台应用程序之间共享数据库? - How to share a database between ASP.NET MVC 5 application and .NET console application? 在ASP.NET Core中的应用程序和子应用程序之间共享Cookie - Share Cookie between application and sub application in ASP.NET Core 如何在父控制台应用程序和ASP.NET Web应用程序之间共享app.config - How to share app.config between parent console application and ASP.NET web application 如何在WPF和ASP.NET MVC应用程序之间共享最多代码? - How to share the most code between a WPF and an ASP.NET MVC application? 如何在Asp.net核心中的ViewComponent之间共享ViewData - How share ViewData between ViewComponent in Asp.net core 如何在两台服务器之间共享 ASP.NET Core 3 中的会话? - How to share Session in ASP.NET Core 3 between 2 servers? 如何在JavaScript中使用asp.net会话? - How to use asp.net sessions in javascript? 在ASP.NET MVC中的多个会话/应用程序实例之间共享对象实例 - Share an object instance between multiple session/application instances in ASP.NET MVC 调试asp.net应用程序时在Visual Studio中查看会话 - View sessions in Visual Studio when debugging a asp.net application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM