112 if ( self::$instance ) {
113 $manager->setupPHPSessionHandler( self::$instance );
118 if ( defined(
'MW_NO_SESSION_HANDLER' ) ) {
119 throw new \BadMethodCallException(
'MW_NO_SESSION_HANDLER is defined' );
123 self::$instance =
new self(
$manager );
126 session_write_close();
129 \Wikimedia\suppressWarnings();
132 ini_set(
'session.use_cookies', 0 );
133 ini_set(
'session.use_trans_sid', 0 );
139 session_cache_limiter(
'' );
142 \Wikimedia\PhpSessionSerializer::setSerializeHandler();
146 session_set_save_handler( self::$instance,
true );
148 \Wikimedia\restoreWarnings();
214 if ( self::$instance !== $this ) {
215 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
217 if ( !$this->enable ) {
218 throw new \BadMethodCallException(
'Attempt to use PHP session management' );
221 $session = $this->manager->getSessionById( $id,
false );
227 $data = iterator_to_array( $session );
228 $this->sessionFieldCache[$id] = $data;
229 return (
string)\Wikimedia\PhpSessionSerializer::encode( $data );
242 public function write( $id, $dataStr ) {
243 if ( self::$instance !== $this ) {
244 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
246 if ( !$this->enable ) {
247 throw new \BadMethodCallException(
'Attempt to use PHP session management' );
250 $session = $this->manager->getSessionById( $id,
true );
254 $this->logger->warning(
255 __METHOD__ .
': Session "{session}" cannot be loaded, skipping write.',
263 $data = \Wikimedia\PhpSessionSerializer::decode( $dataStr );
264 if ( $data ===
null ) {
272 $cache = $this->sessionFieldCache[$id] ?? [];
273 foreach ( $data as $key => $value ) {
274 if ( !array_key_exists( $key,
$cache ) ) {
275 if ( $session->exists( $key ) ) {
277 $this->logger->warning(
278 __METHOD__ .
": Key \"$key\" added in both Session and \$_SESSION!"
282 $session->set( $key, $value );
285 } elseif (
$cache[$key] === $value ) {
287 } elseif ( !$session->exists( $key ) ) {
289 $this->logger->warning(
290 __METHOD__ .
": Key \"$key\" deleted in Session and changed in \$_SESSION!"
292 $session->set( $key, $value );
294 } elseif (
$cache[$key] === $session->get( $key ) ) {
296 $session->set( $key, $value );
300 $this->logger->warning(
301 __METHOD__ .
": Key \"$key\" changed in both Session and \$_SESSION!"
307 \Wikimedia\PhpSessionSerializer::setLogger(
new NullLogger() );
308 foreach (
$cache as $key => $value ) {
309 if ( !array_key_exists( $key, $data ) && $session->exists( $key ) &&
310 \
Wikimedia\PhpSessionSerializer::encode( [ $key =>
true ] )
312 if ( $value === $session->get( $key ) ) {
314 $session->remove( $key );
318 $this->logger->warning(
319 __METHOD__ .
": Key \"$key\" changed in Session and deleted in \$_SESSION!"
324 \Wikimedia\PhpSessionSerializer::setLogger( $this->logger );
330 $this->logger->warning(
'Something wrote to $_SESSION!' );
334 $this->sessionFieldCache[$id] = iterator_to_array( $session );