PHP - Setting SESSION time on already defined functions

Asked

Viewed 527 times

0

I need to set the SESSION time of my site, which I took from another developer.

Session settings are used from a ready-made file, Session.php

Follow the code of that file:

<?php

// classe para gerenciar sessão
Class Session extends Singleton {    
protected function __construct() {
    if(!session_start()) {
        session_start();     
    }
}

public static function define($key,$val) {
    $classe = self::instance();
    $_SESSION[$key] = $val;
}

public static function apaga($key) {
    $classe = self::instance();
    unset($_SESSION[$key]);
}

public static function limpa() {
    $classe = self::instance();
    $_SESSION = array();

}

public static function retorna($key) {
    $classe = self::instance();
    if(!empty($_SESSION[$key])) {
        return $_SESSION[$key];
    } else {
        return false;
    }
}

public static function define_dados($data = array()) {
    if(is_array($data)) {
        foreach($data as $key=>$val) {
            $_SESSION[$key] = $val;
        }
    }
}

public static function fecha() {
    session_write_close();
}
}

?> 

How could I set this time to 1 hour, for example? I tried several things I searched on the internet, but nothing. Someone has some light?

1 answer

1

The logic of sessions is to be destroyed the moment the "browser closes", however it is possible to control the time of the "session" and the "cookie" that maintains the session.

But before I tell you a problem in your code, this is wrong:

protected function __construct() {
    if(!session_start()) {
        session_start();     
    }
}

Actually you’re just calling !session_start, the second will never run, probably you thought that the ! I would only do a check, but this is a mistake, it is a function, if it fails the second will try to execute and will fail as well. Which means it’s useless anyway.

If you want to check if a session is already started you can use session_status (PHP5.4+) or session_id to maintain PHP5.3 compatibility.

With session_id

Works on all versions of PHP5 (I believe it works on PHP7 also, at least in the documentation nothing has changed):

protected function __construct() {
    if (session_id() === '') { //Se for vazio é porque não iniciou a sessão ainda
        session_start();     
    }
}

With session_status

It will work if PHP5.4+:

protected function __construct() {
    if (session_status() !== PHP_SESSION_ACTIVE) {
        session_start();     
    }
}

Increase the lifespan of a session

Now to increase session time you should use session_set_cookie_params and must be carried out before session_start

Note: if it is PHP7 you can configure directly on session_start

protected function __construct() {
    if (session_id() === '') { //Se for vazio é porque não iniciou a sessão ainda
        $expiraem = 3600 * 24 * 1; // 1 dia
        session_set_cookie_params($expiraem);
        session_start();
    }
}

If it is PHP7:

protected function __construct() {
    //Se for vazio é porque não iniciou a sessão ainda
    if (session_id() === '') {
        session_start([
            'cookie_lifetime' => 3600 * 24 * 1 // 1 dia
        ]);
    }
}

I recommend you read the documentation:

NOTE: documentation in Portuguese is very outdated and usually contains some problems, so I am recommending in English.

An example of a problem in the Portuguese documentation: The use_include_path parameter has been replaced by the flags parameter?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.