Accessing a session variable in a class

Is it considered normal to use session_start in the class constructor when you need to get or save session values? Best practics?

The coudn't session variable is not retrieved from the class unless session_start is called from __contruct.

session_start();    

if(isset($_REQUEST['siteid'])){
    $siteid = $_REQUEST['siteid'];
    $_SESSION['siteid'] = $siteid;
    echo $siteid;
}else{
    $siteid = "";
}
require_once 'common/lib_constant.php';
require_once 'common/database.php';
require_once 'common/common.class.php';
$commonClass = new commonClass();
+3
source share
2 answers

Best practice is to start a session from bootstrap.
and the access session itself through some class of smart shells (see examples for an example).
Thus, you should be 100% sure that you will not have problems with the header and duplication of session_start calls in your code.
A class can be created several times / request ... bootstrap is run only once / request.

+3

- , session_start() , , , , , session_start() .

singleton , , , () $Session , .

Session.

0

All Articles