Up until PHP 7, in order to override php.ini settings for secure session management, you had to use a series of ini_set() commands. This approach is extremely annoying in that you also needed to know which settings were available, and being able to re-use the same settings in other applications was difficult. As of PHP 7, however, you can supply an array of parameters to the session_start() command, which immediately sets those values.
Application\Security\SessOptions class, which will hold session parameters and also have the ability to start the session. We also define a class constant in case invalid session options are passed:namespace Application\Security;
use ReflectionClass;
use InvalidArgumentsException;
class SessOptions
{
const ERROR_PARAMS = 'ERROR: invalid session options';php.ini session directives (documented at http://php.net/manual/en/session.configuration.php). We are specifically looking for directives that, in the Changeable column, are marked PHP_INI_ALL. Such directives can be overridden at runtime, and are thus available as arguments to session_start():
const SESS_OP_NAME = 'name'; const SESS_OP_LAZY_WRITE = 'lazy_write'; // AVAILABLE // SINCE PHP 7.0.0. const SESS_OP_SAVE_PATH = 'save_path'; const SESS_OP_SAVE_HANDLER = 'save_handler'; // etc.
php.ini session settings as an argument. We use ReflectionClass to get a list of class constants, and run the $options argument through a loop to confirm the setting is allowed. Also note the use of array_flip(), which flips keys and values, so that the actual values for our class constants form the array key, and the name of the class constant becomes the value:protected $options;
protected $allowed;
public function __construct(array $options)
{
$reflect = new ReflectionClass(get_class($this));
$this->allowed = $reflect->getConstants();
$this->allowed = array_flip($this->allowed);
unset($this->allowed[self::ERROR_PARAMS]);
foreach ($options as $key => $value) {
if(!isset($this->allowed[$key])) {
error_log(__METHOD__ . ':' . self::ERROR_PARAMS);
throw new InvalidArgumentsException(
self::ERROR_PARAMS);
}
}
$this->options = $options;
}public function getAllowed()
{
return $this->allowed;
}
public function start()
{
session_start($this->options);
}Place all the code discussed in this recipe into a SessOptions.php file in the Application\Security directory. You can then define a calling program called chap_13_session_options.php to test the new class, which sets up autoloading and uses the class:
<?php require __DIR__ . '/../Application/Autoload/Loader.php'; Application\Autoload\Loader::init(__DIR__ . '/..'); use Application\Security\SessOptions;
Next, define an array that uses the class constants as keys, with values as desired to manage the session. Note that in the example shown here, session information is stored in a subdirectory, session, which you need to create:
$options = [ SessOptions::SESS_OP_USE_ONLY_COOKIES => 1, SessOptions::SESS_OP_COOKIE_LIFETIME => 300, SessOptions::SESS_OP_COOKIE_HTTPONLY => 1, SessOptions::SESS_OP_NAME => 'UNLIKELYSOURCE', SessOptions::SESS_OP_SAVE_PATH => __DIR__ . '/session' ];
You can now create the SessOptions instance and run start() to start the session. You could use phpinfo() here to show some information on the session:
$sessOpt = new SessOptions($options); $sessOpt->start(); $_SESSION['test'] = 'TEST'; phpinfo(INFO_VARIABLES);
If you look for information on cookies using your browser's developer tools, you will note the name is set to UNLIKELYSOURCE and the expiration time is 5 minutes from now:

If you do a scan of the session directory, you will see that the session information has been stored there:

php.ini directives, see this summary: http://php.net/manual/en/session.configuration.php