简体   繁体   中英

Symfony2: How can i see a list of open sessions

is it possible get a list with opened sessions in Symfony2? I need this to check if one session in particular is opened in this moment.

Thanks.

Wether it is possible to see which sessions have been opened depends on the SessionHandler you are using.

The default session handler used by symfony-standard proxies php's native one and is called NativeFileSessionHandler . This handler is storing the session information in files which makes it hard to extract the session data.

In order to be able to have easy access to the session information you can configure symfony to use one of the database-driven SessionSaveHandlers it provides ( documentation ).

An example on how to implement these can be found in the documentation chapter How to use PdoSessionHandler to store Sessions in the Database .

You can then create a Session entity/repository and query the session information using doctrine as with any other entity. Aswell you could override the default session handler and change the garbage collector ie to mark sessions where the user did not log out regularly for displaying a reminder message.

It's possible to write session data into database by setting an CustomSaveHandler. See documentation: http://symfony.com/doc/current/components/http_foundation/session_configuration.html#custom-save-handlers

After that you can query the session table.

In Controller

class UserController extends 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;
}

}

In Template:

    {% if is_granted('IS_AUTHENTICATED_REMEMBERED') and is_granted('ROLE_ADMIN') %}

        <div class="profile-menu shadowed">{{ render(controller('BaseBundle:User:whoIsOnline')) }}</div>

    {% endif %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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