Increase session lifetime in symfony - php

I have this code in framework.yaml :
session:
handler_id: ~
cookie_lifetime: 43200
gc_maxlifetime: 43200
I want the session to be alive about 12 hours, but after some minutes the session expire. What else modification should I do ? Thx in advance.

If I remember right, your current configuration make PHP save session files inside /var/lib/php/sessions and this directory always checked and PID inside it removed by a cron job configured in php.ini via gc_maxlifetime.
To fixed this issue you have to override the session handler and make Symfony handle the session using handler_id and save_path option as the following:
framework:
session:
handler_id: session.handler.native_file
save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
cookie_lifetime: 43200
gc_maxlifetime: 43200

Related

Why is my Symfony PdoSessionHandler not working?

I'm trying to use the PdoSessionHandler in Symfony 5.4. When I'm following the instructions on the Symfony site nothing happens in the database each time a Symfony Session is called. Even when I'm removing the existing session files, removing the Symfony cache and restarting WAMPserver just to be sure, there are still new session files created.
Nothing appears in the Symfony logging pointing to this issue.
Of course I replaced DATABASE_URL in .env to the values that will work with my database.
My services.yaml looks like this:
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler:
arguments:
- '%env(DATABASE_URL)%'
And my framework.yaml looks like this:
# see https://symfony.com/doc/current/reference/configuration/framework.html
framework:
secret: '%env(APP_SECRET)%'
#csrf_protection: true
http_method_override: false
# Enables session support. Note that the session will ONLY be started if you read or write from it.
# Remove or comment this section to explicitly disable session support.
session:
handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
cookie_secure: auto
cookie_samesite: lax
storage_factory_id: session.storage.factory.native
#esi: true
#fragments: true
php_errors:
log: true
when#test:
framework:
test: true
session:
storage_factory_id: session.storage.factory.mock_file
I created the table with the following query:
CREATE TABLE `sessions` (
`sess_id` VARBINARY(128) NOT NULL PRIMARY KEY,
`sess_data` BLOB NOT NULL,
`sess_lifetime` INTEGER UNSIGNED NOT NULL,
`sess_time` INTEGER UNSIGNED NOT NULL,
INDEX `sessions_sess_lifetime_idx` (`sess_lifetime`)
) COLLATE utf8mb4_bin, ENGINE = InnoDB;
This part will be executed when there is someone logging on:
$session = new Session();
if(!isset($_SESSION)){
$session->start();
}
$session->set('ownerid', $databasestuff);
I see that you are using Session, possibly from the Symfony\Component\HttpFoundation\Session\Session bundle.
I don't think this will work.
You might want to use the autowiring for your controller to wireup the SessionInterface.
For example:
namespace App\Controller;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
.
class DummyController extends AbstractController
{
protected $Session;
public function __construct(SessionInterface $Session)
{
$this->Session = $Session; //Setting the Session interface to the Session var
if(!$this->Session->isStarted()) //Starting the session if not done yet
$this->Session->start();
}
}

Issue with session.handler.native_file session handler in symfony2

I'm facing a really weird issue with an application i'm working on based on symfony2 (2.5).
Long story short:
in the config.yml file i have this:
framework:
...
session:
name: "a_given_name"
# THE FOLLOWING LINE CAUSES THE PROBLEM
handler_id: session.handler.native_file
save_path: "%kernel.root_dir%/sessions"
cookie_lifetime: 2629744 #1 month
gc_maxlifetime: 2629744 #1 month
...
And session does not work!
I checked everything, the system create the files but those files are always empty.
Changing the entry realated to "handler_id" in config.yml to: "~" everything works fine.
framework:
...
session:
# CHANGING TO THE FOLLOWING -> IT WORKS
handler_id: ~
...
But, as stated in the docs, the "session.handler.native_file" is the default hanlder... so it should work anyway...
Any help about it?
Thanks a lot...
I faced the same issue when running Vagrant machine.
I recommend to move sessions out of the synced folders (default /var/www/html) to new location, e.g. /home/symfony/sessions
framework:
session:
handler_id: session.handler.native_file
save_path: "/home/symfony/sessions/%kernel.environment%"
Also, helpful article:
https://coderwall.com/p/h3i5mw/symfony-session-problems-with-vagrant

How can I increase session lifetime in Symfony2?

I have set the gc_maxlifetime in my config.yml file to 300. But its not working. Symfony2 still takes the value set in php.ini
Symfony version is 2.4.4
Try setting the cookie_lifetime parameters of the session config key.
session:
cookie_lifetime: 300 # override php.ini config
as described here
hope this help
Here is my approach (gotten from Drupal configuration):
set handler to native_file (i'd use PDOSessionHanlder, but seems it's not very stable);
create a seesions directory under app folder;
config.yml file:
session:
handler_id: session.handler.native_file
cookie_domain: %cookie_domain%
name: SFSESSID
cookie_lifetime: 2000000 # change this if you want
save_path: "%kernel.root_dir%/sessions"
gc_divisor: 100
gc_maxlifetime: 200000 # change this if you want
gc_probability: 1

Symfony calls the PHP garbage collector on Ubuntu 14.04 even when session.gc_probability is set to 0

As the title state for some reason my Symfony 2.5 Application is calling the php garbage collector even when all of my php.ini files have:
session.gc_probability = 0
Does anyone know how to prevent this from happening?
Error message im getting:
Notice: SessionHandler::gc(): ps_files_cleanup_dir: opendir(/var/lib/php5)
failed: Permission denied (13) in /<path-to-my-site>/var/cache/dev/classes.php line 432
FROM PHPINFO():
Directive Local Value Master Value
session.gc_divisor 1000 1000
session.gc_maxlifetime 86400 86400
session.gc_probability 0 0
I know that i can just give the www-data user permission to the /var/lib/php5 folder or change the session.save_path to somewhere that the www-data user has access to already but i want to know why this process is even getting called when it should be disabled.
I found it, I guess the latest version of symfony is overwriting this by default when using the app_dev.php. The Symfony FrameworkBundle is setting the session.gc_probability = 1.
As of Symfony 3
However, some operating systems do their own session handling and set the session.gc_probability variable to 0 to stop PHP doing garbage collection. That's why Symfony now overwrites this value to 1.
If you wish to use the original value set in your php.ini, add the following configuration:
# config.yml
framework:
session:
gc_probability: null
https://symfony.com/doc/current/components/http_foundation/session_configuration.html#configuring-garbage-collection
Previous 2.x versions
To change this add the following to your config.yml
framework:
session:
gc_probability: 0
Then clear the dev cache
php app/console cache:clear
This is where it shows the gc_probability defaulted to 1. Why they dont just read from the php.ini settings im not sure.
http://symfony.com/doc/2.5/reference/configuration/framework.html#gc-probability
You can set path for sessions manually. See Symfony doc on sessions directory.
# app/config/config.yml
framework:
session:
handler_id: session.handler.native_file
save_path: '%kernel.root_dir%/sessions'

Symfony component caching

I turn on caching the component by editing cache.yml file in my module
_startpage:
enabled: true
contextual: true
lifetime: 60
There was no time difference after that but I think the component is cached because there is a symfony cache box over it (With cache info). I realized that the number of queries to DB is the same as without caching - component for sure execute some queries so why the number of queries did not changed?
Solved.
factories.yml
view_cache:
class: sfFileCache
param:
automatic_cleaning_factor: 0
cache_dir: %SF_TEMPLATE_CACHE_DIR%
lifetime: 86400
prefix: %SF_APP_DIR%/template

Categories