简体   繁体   English

我可以使用php Sessions在多个客户端之间共享Session变量吗?

[英]Can I share Session variables between multiple clients with php Sessions?

what I am trying to find out is, if I can share a Session variable for multiple clients. 我想要找到的是,如果我可以为多个客户共享一个Session变量。 Like they can use the exactly same Object. 就像他们可以使用完全相同的Object。 The below example will illustrate what I would like to do. 下面的例子将说明我想做什么。

client1: 客户端1:

start_session();
include('somelcass.php');
//some code...
$someobj = new someclass();
$_SESSION['myobject'] = serialize($someobj);
$id = sha1("somephrase");
set_session_var_for_other_users_by_id('myobject', $id);

client2: 客户机程序:

start_session();
include('somelcass.php');
$id = sha1("somephrase");
get_sessionvars_from_other_users($id);
$someobj = unserialize($_SESSION['myobject']);
//now use someobj from class someclass

And my additional question is: Do you recommand using some session extention like: sessionPsql 我的另一个问题是:你是否建议使用一些会话扩展,如: sessionPsql

Answering your last question first: 首先回答你的上一个问题:

The Session PgSQL Docs you linked is the PostgreSQL Session Save Handler . 您链接的Session PgSQL DocsPostgreSQL会话保存处理程序 It is a Session Save Handler you can configure to use instead of the default session save handler. 它是一个会话保存处理程序,您可以配置使用而不是默认的会话保存处理程序。 The default session save handler in PHP is storing sessions to disk ( files ). PHP中的默认会话保存处理程序是将会话存储到磁盘( files )。 If you use the save handler for PostgreSQL sessions are saved into a PostgreSQL database instead ( pgsql ). 如果使用PostgreSQL的保存处理程序会话保存到PostgreSQL数据库中( pgsql )。

Saving sessions inside a database can make sense if you want to allow access to the session store from multiple webservers (scaling an application) or in your case (probably) to access all sessions with SQL queries albeit normally a tailored session save handler is defined for that (which could be based on the PgSQL session save handler functions). 如果您希望允许从多个Web服务器(扩展应用程序)或在您的情况下(可能)访问具有SQL查询的所有会话,则可以在数据库中保存会话,尽管通常为定义的会话保存处理程序定义那(可以基于PgSQL会话保存处理函数)。

To answer your first question then: 要回答你的第一个问题:

Yes you can do so as long as you've got a reference to the object you relate to and you know how to access it. 是的,只要您有与您相关的对象的引用并且您知道如何访问它,就可以这样做。 This can be either done by manually accessing the session storage or by sharing a session on it's own and switching sessions to access other session data. 这可以通过手动访问会话存储或通过自己共享会话以及切换会话来访问其他会话数据来完成。 It depends on your needs, in your case it's probably more easy to just access serialized data that is stored by the ID in some extra table that has nothing to do with sessions. 这取决于您的需求,在您的情况下,可能更容易访问由ID存储的序列化数据在一些与会话无关的额外表中。 You should think about how to take care of the data if you don't need it any longer, eg remove it after some time of inactivity. 如果您不再需要它,您应该考虑如何处理数据,例如在一段时间不活动后将其删除。 In the end you're writing your own session implementation that way which is do-able. 最后,您正在以可行的方式编写自己的会话实现。 PHP before version 4 had no session support out of the box and the session support it has nowadays is very lightweight so if you need to do more specific stuff like you need to do, you normally write your own. 在版本4之前的PHP没有开箱即用的会话支持,现在它的会话支持非常轻量级,所以如果你需要做更多特定的事情,你通常会编写自己的。

So multiple clients can use the same session (share a session) which is actually as well a way to attack webapps ( session hijacking Attack ) but as long as the "hijack" is intended inside your application data-flow, I do not see anything technically wrong with it. 因此,多个客户端可以使用相同的会话(共享会话),这实际上也是一种攻击webapps( 会话劫持攻击 )的方法,但只要“劫持”是在应用程序数据流中,我什么都看不到技术上错了。 In PHP that means you need to close the current session, open the other one (sessions are identified by their name and ID), read the value, close the other session and re-open the current one. 在PHP中,这意味着您需要关闭当前会话,打开另一个会话(会话由其名称 ID标识),读取值,关闭其他会话并重新打开当前会话。 It technically works in PHP however write solid code when you do this because session problems are quite hard to debug. 它在技术上适用于PHP,但是在执行此操作时编写可靠的代码,因为会话问题很难调试。

This is also often a good reason to write your own object-sharing mechanism between multiple clients instead of re-using PHP's session feature Docs for that. 这通常也是在多个客户端之间编写自己的对象共享机制的好理由,而不是为此重用PHP的会话功能Doc

Multiple clients can't share data in the session object. 多个客户端无法在会话对象中共享数据。 If you want to share data between clients, you would normally use some other means of server side storage eg A database. 如果要在客户端之间共享数据,通常会使用其他一些服务器端存储方式,例如数据库。

I have written a solution for PHP applications to resolve mainly 2 types of problem: 我已经为PHP应用程序编写了一个解决方案,主要解决了两类问题:

  • How to share data/variables between PHP Process, hosted on same/differents servers 如何在PHP Process之间共享数据/变量,托管在相同/不同的服务器上
  • How to synchronize read/write operations in data/variables 如何在数据/变量中同步读/写操作

My Project is hosted in GitHub ANYEM Project 我的项目托管在GitHub ANYEM项目中

First : Start the ANYEM_SERVER using command line 第一步:使用命令行启动ANYEM_SERVER

php ANYEM/ANYEM_SERVER/anyem.app.server.impl/ServerImpl.php

Now, in your PHP Application you can do as follow: 现在,在PHP应用程序中,您可以执行以下操作:

<?php
// load server's connection configuration (ANYEM_SERVER IP and Port Number ...) 
$clientConnection    = ClientConnectionImpl::newClient();
// build a key for your variable that will be stored in server
// the key is composed on 3 Parts : [1] => URL, [2] => Variable Namespace, [3] => Variable Name
$identifier          = new ResourceIdentifierImpl("anyem.com", "anyemNameSpace", "a");

$a = 5;
$anyemClient         = new AnyemClientImpl($clientConnection, $identifier);
try {
    // if $a is reserved by another PHP Process, so this process
    // will sleep (1/10) seconds and retry the reservation of the resource
    // here, the max number of reservation attempts is 5, if reservation
    // fails, an Exception will be thrown
    $responseWrapper = $anyemClient->get($a, 5, 100000);
    // here we have reserved the variable $a, and we have the unserialized contents
    $a = $responseWrapper->getResource()->getData();
    // here, we update the contents, send it to ANYEM_SERVER and releasing the lock (we unreserve it)
    $anyemClient->put(++$a);
}
catch (Exception $e) {
    print $e->getMessage() . "\n";
    continue;
}

Hope that can helps someone :) 希望可以帮助别人:)

I think best solution for this problem is using database. 我认为这个问题的最佳解决方案是使用数据库。 Create a table and store in it. 创建一个表并存储在其中。 When you need just read data from table. 当您只需要从表中读取数据时。 it is fast and easy solution to share data between sessions. 在会话之间共享数据是快速简便的解决方案。

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

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