Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP Deprecated: Calling session_set_save_handler() in PHP 8.4. #20140

Open
terabytesoftw opened this issue Mar 26, 2024 · 7 comments
Open

PHP Deprecated: Calling session_set_save_handler() in PHP 8.4. #20140

terabytesoftw opened this issue Mar 26, 2024 · 7 comments
Labels

Comments

@terabytesoftw
Copy link
Member

PHP Deprecated: Calling session_set_save_handler() with more than 2 arguments is deprecated in /home/runner/work/yii2/yii2/framework/web/Session.php on line 187

@rob006 @samdark @bizley

This is the last error we have left in PHP 8.4, which would be the best way to fix it.

@rob006
Copy link
Contributor

rob006 commented Mar 26, 2024

https://wiki.php.net/rfc/deprecate_functions_with_overloaded_signatures#session_set_save_handler

Suggested backward compatible alternative: code which calls session_set_save_handler() with 6 or more arguments should wrap the callbacks into a SessionHandlerInterface implementation.

@terabytesoftw
Copy link
Member Author

terabytesoftw commented Mar 27, 2024

class SessionHandlerInterface {
    /* Métodos */
    abstract public close(): bool
    abstract public destroy(string $session_id): bool
    abstract public gc(int $maxlifetime): int
    abstract public open(string $save_path, string $session_name): bool
    abstract public read(string $session_id): string
    abstract public write(string $session_id, string $session_data): bool
}

I will have to add these new methods, and call the old ones the new methods, is it the only way to not break bc?

@samdark
Copy link
Member

samdark commented Apr 2, 2024

Seems so.

@rob006
Copy link
Contributor

rob006 commented Apr 3, 2024

We could create a separate decorator class to avoid adding more methods to Session.

@terabytesoftw
Copy link
Member Author

terabytesoftw commented Jul 23, 2024

This could be a solution to resolve this issue.

<?php

namespace yii\web;

use SessionHandlerInterface;

class SessionHandler implements SessionHandlerInterface
{
    private Session $session;

    public function __construct(Session $session)
    {
        $this->session = $session;
    }

    /**
     * @inheritDoc
     */
    public function open(string $savePath, string $sessionName): bool
    {
        return $this->session->openSession($savePath, $sessionName);
    }

    /**
     * @inheritDoc
     */
    public function close(): bool
    {
        return $this->session->closeSession();
    }

    /**
     * @inheritDoc
     */
    public function read(string $id): string
    {
        return $this->session->readSession($id);
    }

    /**
     * @inheritDoc
     */
    public function write(string $id, string $data): bool
    {
        return $this->session->writeSession($id, $data);
    }

    /**
     * @inheritDoc
     */
    public function destroy(string $id): bool
    {
        return $this->session->destroySession($id);
    }

    /**
     * @inheritDoc
     */
    public function gc(int $maxlifetime): int|false
    {
        return $this->session->gcSession($maxlifetime);
    }
}
class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Countable
    /**
     * @var SessionHandlerInterface|array|null an object implementing the SessionHandlerInterface or a configuration array.
     * If set, will be used to provide persistency instead of build-in methods.
     */
    public SessionHandlerInterface|array|null $handler = null;
    
        /**
     * Registers session handler.
     *
     * @throws \yii\base\InvalidConfigException
     */
    protected function registerSessionHandler()
    {
        $sessionModuleName = session_module_name();

        if (static::$_originalSessionModule === null) {
            static::$_originalSessionModule = $sessionModuleName;
        }

        if ($this->handler === null && $this->getUseCustomStorage()) {
            $this->handler = Yii::createObject(
                [
                    '__class' => SessionHandler::class,
                    '__construct()' => [$this],
                ]
            );
        }

        if ($this->handler !== null) {
            if (is_array($this->handler)) {
                $this->handler = Yii::createObject($this->handler);
            }

            if (!$this->handler instanceof SessionHandlerInterface) {
                throw new InvalidConfigException(
                    '"' . get_class($this) . '::handler" must implement the SessionHandlerInterface.'
                );
            }

            YII_DEBUG
                ? session_set_save_handler($this->handler, false) : @session_set_save_handler($this->handler, false);
        } elseif (
            $sessionModuleName !== static::$_originalSessionModule
            && static::$_originalSessionModule !== null
            && static::$_originalSessionModule !== 'user'
        ) {
            session_module_name(static::$_originalSessionModule);
        }
    }
}

@pavimus
Copy link

pavimus commented Nov 26, 2024

faced this issue when tried out released php 8.4.1

@schmunk42
Copy link
Contributor

For reference, docker test failures with PHP 8.4: https://github.com/yiisoft/yii2-docker/actions/runs/12030640738/job/33538471452 (a lot of session related stuff)

Tests in yii2 repo are green, but output shows also a lot of errors: https://github.com/yiisoft/yii2/actions/runs/12004863459/job/33460517449

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants