0

This is my AppSesionHandler and here I want to know the session working time then try to make a specific time for it for example 30 min. Here is my class:

<?php
define('SESSION_PATH', dirname(realpath(__FILE__)));
class AppSessionHandler extends SessionHandler
{
    private $sessionLifeTime = 0;
    private $sessionSavePath = SESSION_PATH;
    private $sessionName = 'MyDefsultSession';
    private $sessionPath = '/';
    private $sessionDomain = 'phpdev';
    private $httpOnly = true;
    private $secure = false;
    private $ttl = 1;
    public function __construct()
    {
        // default settings for session 
        ini_set('session.use_cookies', 1);
        ini_set('session.use_only_cookies', 1);
        ini_set('session.use_trans_id', 0);
        ini_set('session.save_handler', 'files');
        session_set_cookie_params($this->sessionLifeTime, $this->sessionPath, $this->sessionDomain, $this->secure, $this->httpOnly);
        session_name($this->sessionName);
        session_save_path($this->sessionSavePath);
        session_set_save_handler($this, true);
    }
    public function __set($key, $value)
    {
        $_SESSION[$key] = $value;
    }
    public function __get($key)
    {
        return (array_key_exists($key, $_SESSION)) ? $_SESSION[$key] : false;
    }
    // start the session  
    public function start()
    {
        if (session_id() === '') {
            if (session_start()) {
                if (!isset($this->sessionStartTime)) {
                    $this->sessionStartTime = time();
                }
            }
        }
    }


    public function read($id): string
    {
        return base64_decode(parent::read($id));
    }
    public function write($id, $data): bool
    {
        return parent::write($id, base64_encode($data));
    }
}
$session = new AppSessionHandler();
$session->start();
$_SESSION['user'] = 'admin';
$_SESSION['password'] = '12334555';
$_SESSION['email'] = 'xx@xx.zz';
$_SESSION['logged'] = true;
echo time()- $session->sessionStartTime;

So at the last line, it always gives me 0. How to make session start time a fixed value?

Dharman
  • 30,962
  • 25
  • 85
  • 135
sHR
  • 1
  • 2
  • Does this answer your question? [How do I expire a PHP session after 30 minutes?](https://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes) – Oliver Kucharzewski Dec 15 '21 at 03:20
  • No, This question is about the closing session,But here I need to define the session start time – sHR Dec 15 '21 at 17:13
  • What have you tried to resolve the problem? I don't see any code where you read `sessionStartTime` from the session – Nico Haase Dec 16 '21 at 09:39

0 Answers0