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

时间:2023-01-07 16:48:51

My company took some old php application over. 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. For implementing new features, we will create an Asp.net application that has the same look to the user. We want to develop a kind of 'coexisting' web application. 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,并且没有来自前一个开发人员的任何文档,所以我们不希望在PHP开发上花费太多的资源。为了实现新的特性,我们将创建一个Asp.net应用程序,它的外观与用户相同。我们希望开发一种“共存”的web应用程序。因此,我们必须在PHP和Asp.net webapplication项目之间共享会话,因为有一个与现有MySQL数据库相关的用户管理。

(e.g. 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

编辑:iis6将是我们的目标服务器,altough iis7.5也是一个选项

7 个解决方案

#1


13  

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). 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应用程序启动(由于PHP应用程序仍然是主要应用程序这一事实)。新的会话将导致日志表中的新条目。PHP应用程序中的每个链接都链接到ASP。Net应用程序包含get参数、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.

ASP.net应用程序检查获取参数并在ASP中设置IDUser。如果get参数指向一个现有会话,则为Net会话。

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)

总而言之,我认为我的方法是有用的,而且自从部署以来,没有一个客户抱怨过(1年前)

#2


2  

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。可以读取这些字段并将它们加载到ASP中的网页。网络会议。

Theoretically it's possible.

理论上这是有可能的。

#3


2  

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. The DB is a precious resource, and there's really no reason to thrash it just for session data.

首先,我认为在数据库服务器(如mysql或SQL server)中存储会话数据是个坏主意。DB是一种宝贵的资源,没有理由仅仅为了会话数据而对它进行研究。

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. Then store that key in a cookie called session-id or something.

基本上,使用memcached时需要做的就是为键生成一个伪随机令牌,然后执行memcache .set。然后将该键存储在一个名为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,以及使用简单的memcache .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.

这是一种非常简单和可伸缩的方式来处理会话,并且几乎可以使用任何语言/服务器。

#4


0  

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

http://cz.php.net/manual/en/function.session-set-save-handler.php 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作为会话的后端。

#5


0  

Not a big deal.
Your asp app should do a three simple things:

不是一个大问题。你的asp应用程序应该做三件简单的事情:

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

#6


0  

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

参见:对.NET/Windows和PHP/Linux/Apache web站点都使用OpenID

#7


-1  

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. The session is identified usually by a cookie with name PHPSESSID.

默认情况下,PHP将其会话数据作为序列化数组写入到根据会话命名的文件中。会话通常由名为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). Or change the name the cookie used to store the session.

然而,实现您自己的PHP会话处理程序来以任何格式/到您喜欢的任何存储中编写文件是非常容易的(请参阅auto-prepend,了解如何将撤销的代码与每个页面关联起来,而不必重写每个页面)。或者更改用于存储会话的cookie的名称。

C.

C。

#1


13  

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). 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应用程序启动(由于PHP应用程序仍然是主要应用程序这一事实)。新的会话将导致日志表中的新条目。PHP应用程序中的每个链接都链接到ASP。Net应用程序包含get参数、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.

ASP.net应用程序检查获取参数并在ASP中设置IDUser。如果get参数指向一个现有会话,则为Net会话。

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)

总而言之,我认为我的方法是有用的,而且自从部署以来,没有一个客户抱怨过(1年前)

#2


2  

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。可以读取这些字段并将它们加载到ASP中的网页。网络会议。

Theoretically it's possible.

理论上这是有可能的。

#3


2  

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. The DB is a precious resource, and there's really no reason to thrash it just for session data.

首先,我认为在数据库服务器(如mysql或SQL server)中存储会话数据是个坏主意。DB是一种宝贵的资源,没有理由仅仅为了会话数据而对它进行研究。

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. Then store that key in a cookie called session-id or something.

基本上,使用memcached时需要做的就是为键生成一个伪随机令牌,然后执行memcache .set。然后将该键存储在一个名为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,以及使用简单的memcache .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.

这是一种非常简单和可伸缩的方式来处理会话,并且几乎可以使用任何语言/服务器。

#4


0  

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

http://cz.php.net/manual/en/function.session-set-save-handler.php 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作为会话的后端。

#5


0  

Not a big deal.
Your asp app should do a three simple things:

不是一个大问题。你的asp应用程序应该做三件简单的事情:

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

#6


0  

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

参见:对.NET/Windows和PHP/Linux/Apache web站点都使用OpenID

#7


-1  

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. The session is identified usually by a cookie with name PHPSESSID.

默认情况下,PHP将其会话数据作为序列化数组写入到根据会话命名的文件中。会话通常由名为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). Or change the name the cookie used to store the session.

然而,实现您自己的PHP会话处理程序来以任何格式/到您喜欢的任何存储中编写文件是非常容易的(请参阅auto-prepend,了解如何将撤销的代码与每个页面关联起来,而不必重写每个页面)。或者更改用于存储会话的cookie的名称。

C.

C。