簡體   English   中英

Symfony2:如何查看打開的會話列表

[英]Symfony2: How can i see a list of open sessions

可以在Symfony2中獲取打開的會話列表嗎? 我需要它來檢查當前是否特別打開了一個會話。

謝謝。

能否查看哪些會話已打開取決於您使用的SessionHandler

symfony-standard代理使用的默認會話處理程序是php的本機會話,稱為NativeFileSessionHandler 該處理程序將會話信息存儲在文件中,這使得很難提取會話數據。

為了能夠輕松訪問會話信息,可以將symfony配置為使用它提供的數據庫驅動的SessionSaveHandlers之一( 文檔 )。

在文檔一章如何使用PdoSessionHandler在數據庫中存儲會話中可以找到有關如何實現這些的示例。

然后,您可以創建Session實體/存儲庫,並像其他任何實體一樣使用教義來查詢會話信息。 同樣,您可以覆蓋默認的會話處理程序並更改垃圾收集器,即標記用戶未定期注銷以顯示提醒消息的會話。

可以通過設置CustomSaveHandler將會話數據寫入數據庫。 請參閱文檔: http : //symfony.com/doc/current/components/http_foundation/session_configuration.html#custom-save-handlers

之后,您可以查詢會話表。

在控制器中

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 %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM