PHP - Setting SESSION time in already defined functions

0

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

Session definitions are used from a ready file, session.php

Follow the code for this 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. Does anyone have any light?

    
asked by anonymous 12.02.2016 / 14:49

1 answer

1

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

But first I'll mention a problem in your code, this is wrong:

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

Actually you are only calling !session_start , the second will never run, you probably thought that ! would only do one check, but this is a mistake, it is a function, if it fails the second will attempt to execute and will fail as well. In other words, it's no good at all.

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

With session_id

It 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 it is PHP5.4 +:

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

Increase the lifetime of a session

Now to increase session time you should use session_set_cookie_params and should be run before% w / w%

  

Note: If you are 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's 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 it in English.

     

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

    
12.02.2016 / 15:18