【问题标题】:Symfony2: How can i see a list of open sessionsSymfony2:我如何查看打开的会话列表
【发布时间】:2013-09-27 10:18:15
【问题描述】:

是否有可能在 Symfony2 中获取已打开会话的列表?我需要这个来检查此时是否特别打开了一个会话。

谢谢。

【问题讨论】:

  • 请定义“打开”会话
  • 此时登录我网站的用户。
  • 你不能这样做。你只能通过在数据库中记录他们的最后活动时间来知道谁在某个时间段内活动。
  • 谢谢,在这种情况下,我如何检查用户是否已登录?我有一个会话的令牌,我需要检查这个会话是否在这一刻打开。谢谢!

标签: php session symfony session-variables


【解决方案1】:

是否可以查看已打开的会话取决于您使用的SessionHandler

symfony-standard 使用的默认会话处理程序代理 php 的本机处理程序,称为NativeFileSessionHandler。此处理程序将会话信息存储在文件中,这使得提取会话数据变得困难。

为了能够轻松访问会话信息,您可以将 symfony 配置为使用它提供的数据库驱动的 SessionSaveHandler 之一 (documentation)。

有关如何实现这些的示例可以在文档章节How to use PdoSessionHandler to store Sessions in the Database 中找到。

然后,您可以创建一个Session 实体/存储库,并像使用任何其他实体一样使用原则查询会话信息。您还可以覆盖默认会话处理程序并更改垃圾收集器,即标记用户未定期注销的会话以显示提醒消息。

【讨论】:

    【解决方案2】:

    可以通过设置 CustomSaveHandler 将会话数据写入数据库。见文档:http://symfony.com/doc/current/components/http_foundation/session_configuration.html#custom-save-handlers

    之后就可以查询会话表了。

    【讨论】:

      【解决方案3】:

      在控制器中

      类 UserController 扩展 BaseController {

      /**
       * Constructor
       */
      public function __construct() {
      
          parent::__construct();
      
      }
      
      /**
       * @return \Symfony\Component\HttpFoundation\Response
       */
      public function whoIsOnlineAction() {
      
          $session_dir = ini_get('session.save_path');
      
          $sessions = preg_grep('/^([^.])/', scandir($session_dir));
      
          $logged_in_user_array = array();
      
          $one_hour_ago = strtotime( '-1 hour');
      
          foreach( $sessions as $key => $session ) {
      
              $session_file_contents = file_get_contents( $session_dir . '/' . $session );
      
              $decoded_array = $this->unserialize_php( $session_file_contents, '|' );
      
              $updated =  $decoded_array['_sf2_meta']['u'];
      
              if( !is_null($decoded_array['_sf2_attributes']['_security_secured_area'] ) ){
      
                  $decoded_array2 = self::unserialize_php( $decoded_array['_sf2_attributes']['_security_secured_area'], '"' );
      
                  $keys = array_keys($decoded_array2);
      
                  if( $one_hour_ago < $updated ) $logged_in_user_array[$decoded_array2 [$keys[4]][0].''] = str_replace( array( 'domain1\\', 'domain2\\'), '', $decoded_array2 [$keys[4]][0].'');
      
              }
          }
      
          return $this->render( 'HubBundle:Core:active_users.html.twig', array(
              'users' => $logged_in_user_array
          ) );
      
      }
      
      /**
       * @param $session_data
       * @param $delimiter
       * @return array
       * @throws \Exception
       */
      private static function unserialize_php($session_data, $delimiter) {
          $return_data = array();
          $offset = 0;
          while ($offset < strlen($session_data)) {
              if (!strstr(substr($session_data, $offset), $delimiter)) {
                  throw new \Exception("invalid data, remaining: " . substr($session_data, $offset));
              }
              $pos = strpos($session_data, $delimiter, $offset);
              $num = $pos - $offset;
              $varname = substr($session_data, $offset, $num);
              $offset += $num + 1;
              $data = unserialize(substr($session_data, $offset));
              $return_data[$varname] = $data;
              $offset += strlen(serialize($data));
          }
          return $return_data;
      }
      

      }

      在模板中:

          {% if is_granted('IS_AUTHENTICATED_REMEMBERED') and is_granted('ROLE_ADMIN') %}
      
              <div class="profile-menu shadowed">{{ render(controller('BaseBundle:User:whoIsOnline')) }}</div>
      
          {% endif %}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-15
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多